简体   繁体   English

使用 Pandas 在 Python 中读取 .xlsx 和 .csv 文件时出现问题

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

I opened and manipulated a.csv file.我打开并操作了一个.csv 文件。 It contains Cyrillic script.它包含西里尔字母。 When I try to open and to save it as a.xlsx file i get an error.当我尝试打开并将其另存为 .xlsx 文件时,出现错误。 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当我保存新的.csv 文件并打开它时,西里尔文脚本会变成随机字符并且实际上不可读(Ангел、ДимиС、СЉСЂ、Мария 等)你可以看到我得到了什么

What should I do?我应该怎么办?

Both output files opened in Excel correctly with this. output 文件都在 Excel 中正确打开。 Note that .to_excel() requires an additional Python package to write Excel files.请注意, .to_excel()需要额外的 Python package 来写入 Excel 文件。 I used pip install openpyxl :我用pip install openpyxl

input.csv:输入。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.仅供参考, .to_csv()不能单独使用utf8 ,但.to_excel()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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