简体   繁体   中英

Having Problems reading .xlsx and .csv files in Python using Pandas

I opened and manipulated a.csv file. It contains Cyrillic script. When I try to open and to save it as a.xlsx file i get an error. When I save the new.csv file and open it, the Cyrillic script turns into random characters and is practically unreadable (Ангел, Димитър, Мария etc.) You can see what i get as a result

What should I do?

Both output files opened in Excel correctly with this. Note that .to_excel() requires an additional Python package to write Excel files. I used pip install openpyxl :

input.csv:

Колонка1,Колонка2,Колонка3
Раз,два,три

Code:

import pandas as pd

data = pd.read_csv('input.csv',encoding='utf-8-sig') # or whatever the actual encoding
data.to_csv('output.csv',encoding='utf-8-sig')
data.to_excel('output.xlsx',encoding='utf-8-sig')

FYI, .to_csv() did not work with utf8 alone, but .to_excel() did.

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