使用Selenium时解决Python 3.6中的UnicodeEncodeError

时间:2018-06-29 19:59:34

标签: python character-encoding

我编写了一个抓取程序,偶尔遇到希腊字母“ mu”:

Traceback (most recent call last):

  File "<ipython-input-1-64e1ce177c3a>", line 1, in <module>
    runfile('C:/Users/jmiller/Desktop/MasterControl Scraping/Project_TEST.py', wdir='C:/Users/jmiller/Desktop/MasterControl Scraping')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/jmiller/Desktop/MasterControl Scraping/Project_TEST.py", line 271, in <module>
    writer.writerow(row)

  File "C:\ProgramData\Anaconda3\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 '\u03bc' in position 166: character maps to <undefined>

我已经读到我应该使用utf-8,但是我的Python程序已经在utf-8中进行编码,因此我不确定在其他地方要指定什么:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jun 13 09:45:52 2018

@author: jmiller
"""

编辑:完成后,我会将它们全部写到一个csv文件中:

with open('combined_asset_info.csv', 'w', newline = '') as myfile:
    writer = csv.writer(myfile, delimiter = ',')
    for row in combined_asset_info:
        writer.writerow(row)

有指针吗?谢谢

1 个答案:

答案 0 :(得分:0)

尝试添加:

with open('combined_asset_info.csv', 'w', newline = '', encoding='cp1252') as myfile:

可能有效。