在Python和UnicodeDecodeError中读取大型excel文件:

时间:2017-02-17 06:16:41

标签: python excel

我是Python的新手,我试图在python中读取一个大的excel文件。我将我的xlsx文件转换为csv以使用pandas。我写了下面的代码:

class VehicleThatCouldBeParked {}
class Car extends VehicleThatCouldBeParked {}
class Van extends VehicleThatCouldBeParked {}

...
VehicleThatCouldBeParked []parking = ne VehicleThatCouldBeParked[100]; // build a parking with hundred locations
...
Car myCar = new Car(); // some car
...
Van myVan = new Van(); // some van
...
parking[76] = myCar; // my car is parked in slot 77
parking[34] = myVan; // my van is parked in slot 35
...

但它给出了这个错误: import pandas as pd pd.read_csv('filepath.csv') df = csv.parse("Sheet") df.head()

你能告诉我为什么会出现这个错误吗?或者您有什么建议来阅读大型Excel文件?我也尝试使用UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 28: character maps to <undefined>模块,但由于我的Python版本,我无法使用openpyxl。(我使用的是Python 2.7.8)

2 个答案:

答案 0 :(得分:0)

尝试

pd.read_csv('filepath.csv',encoding ='utf-8')

还有许多其他编码技术,例如encoding = 'iso-8859-1'encoding = 'cp1252'encoding = 'latin1'。您可以根据自己的要求进行选择。

答案 1 :(得分:0)

使用Microsoft Excel将Excel保存到Unicode文本文件中。

enter image description here

使用以下行打开文件:

df = pd.read_csv(filename,sep='\t',encoding='utf-16-le')
print(df.head())