我有一个 JSON 文件,其中包含这样的文本
.....wax, and voila!\u00c2\u00a0At the moment you can't use our ...
我的简单问题是如何将这些 \u 代码转换(而不是删除)为空格、撇号等...?
Input:一个文本文件.....wax, and voila!\u00c2\u00a0At the moment you can't use our ...
Output: .....wax, and voila!(converted to the line break)At the moment you can't use our ...
Python代码
def TEST():
export= requests.get('https://sample.uk/', auth=('user', 'pass')).text
with open("TEST.json",'w') as file:
file.write(export.decode('utf8'))
我尝试过的:
- 使用 .json()
- 组合 .encode().decode() 等的任何不同方式。
Edit 1
当我将此文件上传到 BigQuery 时,我有 -Â
symbol
更大的样本:
{
"xxxx1": "...You don\u2019t nee...",
"xxxx2": "...Gu\u00e9rer...",
"xxxx3": "...boost.\u00a0Sit back an....",
"xxxx4": "\" \u306f\u3058\u3081\u307e\u3057\u3066\"",
"xxxx5": "\u00a0\n\u00a0",
"xxxx6": "It was Christmas Eve babe\u2026",
"xxxx7": "It\u2019s xxx xxx\u2026"
}
Python代码:
import json
import re
import codecs
def load():
epos_export = r'{"xxxx1": "...You don\u2019t nee...","xxxx2": "...Gu\u00e9rer...","xxxx3": "...boost.\u00a0Sit back an....","xxxx4": "\" \u306f\u3058\u3081\u307e\u3057\u3066\"","xxxx5": "\u00a0\n\u00a0","xxxx6": "It was Christmas Eve babe\u2026","xxxx7": "It\u2019s xxx xxx\u2026"}'
x = json.loads(re.sub(r"(?i)(?:\\u00[0-9a-f]{2})+", unmangle_utf8, epos_export))
with open("TEST.json", "w") as file:
json.dump(x,file)
def unmangle_utf8(match):
escaped = match.group(0) # '\\u00e2\\u0082\\u00ac'
hexstr = escaped.replace(r'\u00', '') # 'e282ac'
buffer = codecs.decode(hexstr, "hex") # b'\xe2\x82\xac'
try:
return buffer.decode('utf8') # '€'
except UnicodeDecodeError:
print("Could not decode buffer: %s" % buffer)
if __name__ == '__main__':
load()