简体   繁体   中英

Getting around UnicodeEncodeError in Python 3.6 while using Selenium

I've written a scraping program that occasionally encounters the Greek letter "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>

I've read that I should use utf-8, but my Python program already is coding in utf-8, so I'm not exactly sure where else to specify that:

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

@author: jmiller
"""

EDIT: I'm writing it all to a csv file when it's complete:

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

Any pointers? Thanks

Try Adding:

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

It might work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM