我有一个简单(但非常困难)的问题。
我正在寻找一种转换包含这种表情符号代码(\ud83d\udc40)
的文本文件并将其替换为将包含-实际表情符号的文本文件
E.G。
with open(OUTPUT, "r+") as infileInsight:
insightData = infileInsight.read()\
.replace('\ud83d\udc40','')\
......
with open(OUTPUT, "w+") as outfileInsight:
outfileInsight.write(insightData)
关于它是重复的: 如果我这样做:
with open(OUTPUT, "r+") as infileInsight:
insightData = infileInsight.read()\
.replace('\ud83d\udc40','')\
......
with open(OUTPUT, "w+") as outfileInsight:
outfileInsight.write(insightData.decode('unicode-escape'))
我有一个错误: UnicodeEncodeError:'ascii'编解码器无法在位置30编码字符u'\ u2600':序数不在范围内(128)
答案 0 :(得分:0)
您只需要ensure_ascii=False
中的json.dump
选项。
如果首先要创建此文件,只需传递该选项即可。
如果其他人给了您这个JSON文件,并且您想将其更改为直接在字符串中使用Unicode字符(与现在的Unicode转义相反),则可以执行以下操作:
import json
with open('input.txt', 'r') as infile:
with open('output.txt', 'w') as outfile:
for line in infile:
data = json.loads(line)
json.dump(data, outfile, ensure_ascii=False)
outfile.write('\n')