0

I am converting excel(xlsx) file to CSV using PYTHON and i am getting the following error message.

Code: import xlrd import csv

with xlrd.open_workbook('a_file.xlsx') as wb:
sh = wb.sheet_by_index(0)  # or wb.sheet_by_name('name_of_the_sheet_here')
with open('a_file.csv', 'wb') as f:
    c = csv.writer(f)
    for r in range(sh.nrows):
        c.writerow(sh.row_values(r))

Error Message: Traceback (most recent call last): File "C:\Python27\Scripts\1.py", line 9, in c.writerow(sh.row_values(r)) UnicodeEncodeError: 'ascii' codec can't encode character u'\xbd' in position 4: ordinal not in range(128)

Any light on resolving the error?

1 Answer 1

2
import pandas as pd
data_xls = pd.read_excel('a_file.xls', 'Sheet1', index_col=None)
data_xls.to_csv('a_file.csv', encoding='utf-8')

You can easily use pandas

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.