Hi
JSON裡有段資料:
"url": "\ /test\ /tr\ /Session\ /122544", ←忽略空格
我要將這段資料load進來再放到新的檔案裡, 程式大概如下:
json_array = json.load(input_file)
for item in json_array:
result.append(item)
with open(output_file, 'w') as outfile:
json.dump(result, outfile, indent = 4)
可是會變成:
"url": "/test/tr/Session/122544",
我試過load進來後再呼叫replace:
str.replace('/', '\/')
可是會變成:
"url": "\ \ /test\ \ /tr\ \ /Session\ \ /122544", ←忽略空格
請問要如何保持最一開始的格式呢
Thanks
import json
origin = '{"url": "\/test\/tr\/Session\/122544"}'
print("origin = "+origin)
datastore = json.loads(origin)
jsonstr = json.dumps(datastore)
print("json = "+jsonstr)
str = jsonstr.replace("/","\/")
print("final = "+str)