這是我的代碼
f = open('test.txt','w')
f.write("\N{Circled White Star}")
f.close
我得到這個錯誤
Traceback (most recent call last):
File "f:/mc experiment/python/SkyblockSniper-main/df.py", line 3, in <module>
f.write("\N{Circled White Star}")
File "F:\programing\python\lib\encodings\cp1252.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\u272a' in position 0: character maps to <undefined>
我所期待的
test.txt 檔案應該有
?
我得到了什么
<nothing>
請幫忙
uj5u.com熱心網友回復:
嘗試更改您打開的檔案的編碼。UTF-8 適用于我的測驗。 您還應該使用背景關系管理器而不是您的方式打開檔案。
star = "?"
with open('test.txt', 'w', encoding="UTF-8") as f:
f.write(f"\n{star}")
uj5u.com熱心網友回復:
無法訪問原始圓形白星符號,unicode 可能有用
star = '\u272A'
with open('test.txt', 'w', encoding="UTF-8") as f:
f.write(star)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/367726.html
標籤:蟒蛇-3.x
上一篇:更新模塊中變數的值
