我无法正确编码和解码包含单引号和双引号的字符串。注意:我需要显示引号。带引号的编码/解码字符串
我将以下字符串保存在txt文件中。
Here’s their mantra: “Eat less and exercise more. The secret to weight loss is energy balance. There are no good or bad calories. It’s all about moderation.”
with open ("file.txt", "r") as myfile:
data = myfile.read()
myfile.close()
print data
the result:

HereΓÇÖs their mantra: ΓÇ£Eat less and exercise more. The secret to weight loss is energy balance. There are no good or bad calories. ItΓÇÖs all about moderation.ΓÇ¥
我完全可以省略引号,但我需要向他们展示
print data.decode('ascii', 'ignore')
Heres their mantra: Eat less and exercise more. The secret to weight loss is energy balance. There are no good or bad calories. Its all about moderation.
print json.dumps(data)
"\ufeff\nHere\u2019s their mantra: \u201cEat less and exercise more. The secret to weight loss is energy balance. There are no good or bad calories. It\u2019s all about moderation.\u201d "
您的控制台或终端编码不支持UTF-8(输入文件的编码)。您的控制台改为使用cp437。 –
那么,我该怎么做? –
你在做什么?您的控制台编码不支持文本中的“花哨”引号;你可以用ASCII等价物替换它们,或者你可以改变你的控制台编码。 –