1
我想將可以保存到文件中的一個字符串python列表加入到一個字符串中,編號爲'\n'.join(self.product.features)
。清單看起來像這樣;Python無法在位置0編碼字符' u3010':字符映射到<undefined>
[
"【SIX IN ONE】This digital radio alarm clock combines 6 functions: Digital clock 12/24 Hour Format for checking time; dual alarm clock with individual alarm volume control for awaking you up and you can adjust the alarm volume level; FM radio for listening to news& weather forecast; auto brightness & 3 steps dimmer control for eyes care; USB charging port for mobile device and easy to charge your phone near bedside; 3.5 mm jack (not included) for external audio source.",
"【LARGE BRIGHT DISPLAY】Large 1.4-inch Cyan Blue LED display without any blink makes time easy to read from a far distance, auto brightness & 3 steps dimmer control for eyes caring, auto set the display to a brighter setting in daytime and softer one at night.",
"【AUTO TIME SET】 Once you plugged this radio alarm clock to the AC Outlet, default EST time will be displayed. DST (Daylight Saving Time) will be switching automatically, Simple Time Zone Alignment (Press and hold SET button then adjust the Hour by Tune Up or Down), Backup Battery to maintain Time and Alarm Setting.",
"【SUITABLE FOR HOME&OFFICE】You can place this radio alarm clock on bedside table; Office desk; kitchen; study table or cabinet at sitting room - Need to connect to main power.",
"【30 DAYS MONEY BACK GUARANTEE】Please feel free to contact us if you have any questions on this radio alarm clock and you can buy with confidence at any time thanks to our 30-day money back guarantee."
]
這是我的代碼,它試圖加入字符串並保存它;
txtfile = open(self.productDir + "\\Product Details.txt", "w+")
...
txtfile.write("\n\n")
if (self.product.features and len(self.product.features) > 0):
txtfile.write('\n'.join(self.product.features))
else:
txtfile.write('Has no features')
...
txtfile.close()
但我得到錯誤;
UnicodeEncodeError: 'charmap' codec can't encode character '\u3010' in position 0: character maps to <undefined>
我可以看到一些字符無法解碼我只是不知道如何在這種情況下使用它來解碼/編碼或繞過它。
只需設置在打開功能編碼名稱(比如'utf-8') ,默認情況下python使用系統編碼 –
是的工作'txtfile = open(self.productDir +「\\ Product Details.txt」,「w +」,encoding =「utf-8」)' – LogicDev
另外我建議使用'with'建設,它不是連接到你的問題,但代碼會更清楚。 - http://www.pythonforbeginners.com/files/with-statement-in-python –