我無法正確編碼和解碼包含單引號和雙引號的字符串。注意:我需要顯示引號。帶引號的編碼/解碼字符串
我將以下字符串保存在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等價物替換它們,或者你可以改變你的控制檯編碼。 –