2017-10-16 116 views
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> 

我可以看到一些字符無法解碼我只是不知道如何在這種情況下使用它來解碼/編碼或繞過它。

+1

只需設置在打開功能編碼名稱(比如'utf-8') ,默認情況下python使用系統編碼 –

+0

是的工作'txtfile = open(self.productDir +「\\ Product Details.txt」,「w +」,encoding =「utf-8」)' – LogicDev

+0

另外我建議使用'with'建設,它不是連接到你的問題,但代碼會更清楚。 - http://www.pythonforbeginners.com/files/with-statement-in-python –

回答

0

當您用反斜槓(\)編寫您的路徑時,我假設您正在使用Windows。在Windows命令行界面(所謂的控制檯)中,編碼當前是cp1252(或其他8位編碼)。字符U + 3010(LEFT BLACK LENTICULAR BRACKET)不存在於cp1252字符集中(並且可能不會在Windows中的任何常見8位字符集中退出)。

可能的解決方法:

  • 轉換來回忽略不可映射字符 - 括號將消失...

    charset = 'ascii' 
    txt = '\n'.join(self.product.features).encode(charset, 'ignore').decode(charset) 
    txtfile.write(txt) 
    
  • 更換一個明智相當於得罪的人物 - 括號將顯示爲()但如果存在其他非映射字符則會中斷字符串

    txt = '\n'.join(self.product.features).replace('\u3010', '(').replace('\u3011', ')') 
    txtfile.write(txt) 
    

恕我直言,最好做的,如果你系統使用它來結合兩者,optionnaly使用cp1252字符集:

txt = '\n'.join(self.product.features).replace('\u3010', '(').replace('\u3011', ')') 
    charset = 'cp1252' 
    txtfile.write(txt.encode(charset, 'ignore').decode(charset)) 
相關問題