简体   繁体   中英

Python gives UnicodeEncodeError reading xlsx

When running this Python code:

import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import unicodedata
import codecs

DataFile=pd.read_excel('indata.xlsx')#,dtype={'text': unicode})

for Rownumber in range (0,7):# 2? til 23761?
 print('rownumber: ', Rownumber)
 ColumnF=str((DataFile.iloc[Rownumber,5]))
 print('ColumnF: ', ColumnF)        
print('EOF')

I get the following error:

('rownumber: ', 0)
Traceback (most recent call last):
  File "example.py", line 13, in <module>
    ColumnF=str((DataFile.iloc[Rownumber,5]))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe6' in position 11: ordinal not in range(128)

any idea how to make Python read xlsx containing non-ascii utf8 caharacters in xlsx?

Try opening the excel with an encoding:

EX :

DataFile=pd.read_excel('indata.xlsx', encoding = 'utf8')

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