[英]Encode UTF-8 for list
我正在使用硒從javascript對象檢索列表。
search_reply = driver.find_element_by_class_name("ac_results")
嘗試寫入csv時,出現以下錯誤:
Traceback (most recent call last):
File "insref_lookup15.py", line 54, in <module>
wr_insref.writerow(instrument_name)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe4' in position 22: ordinal not in range(128)
我曾嘗試將.encode(“ utf-8”)放在這兩者上:
search_reply = driver.find_element_by_class_name("ac_results").encode("utf-8")
和
wr_insref.writerow(instrument_name).encode("utf-8")
但我只得到消息
AttributeError: 'xxx' object has no attribute 'encode'
您需要對列表中的元素進行編碼:
wr_insref.writerow([v.encode('utf8') for v in instrument_name])
csv
模塊文檔的“ 示例”部分涵蓋了更詳細地編寫Unicode對象的過程,其中包括可自動處理此問題的實用程序類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.