2017-08-15 240 views
2

我有一个csv文件是通过将Tableau表导出到csv生成的,但我无法设法在Python中打开它。读取csv文件到熊猫失败

我试图使用pd.read_csv但失败。

import pandas as pd 

#path to file 
path = "tableau_crosstab.csv" 

data = pd.read_csv(path, encoding="ISO-8859-1") 

这适用于文件中读取,但结果只是一个数字,每行一个字符,并在帧的头部一些奇怪的字符行。

ÿþd 
o  
m  
a  
i 

等等。当我尝试导入Excel中的文件,我必须选择选项卡作为分隔符,但是当我索引树,在这里它失败

import pandas as pd 

#path to file 
path = "tableau_crosstab.csv" 

data = pd.read_csv(path, encoding="ISO-8859-1", sep='\t') 

CParserError:错误符号化数据。 C错误:第7行预计1字段,锯2

我曾尝试打开带有编解码器的文件,然后它说编码是'cp1252',但使用它作为编码也失败了。

我也尝试使用utf-8来阅读它,并且也失败了。 我正在想出如何解决这个问题。

下面是其中一个副本,如果该文件是,如果有人可以看看链接 http://www.mediafire.com/file/6dtxo2deczwy3u2/tableau_crosstab.csv

回答

3

你有统一BOM专门utf-16LE

尝试

data = pd.read_csv(path, encoding="utf-16", sep='\t') 

有趣的人物你请参阅:ÿþ对应于十六进制FF FE,它是unicode-16小端字节顺序标记。如果你看到维基百科的页面它显示了所有不同的字节顺序标志着

我让你读书时的CSV如下:

In[4]: 
data = pd.read_csv(r'C:\tableau_crosstab.csv', encoding='utf-16', sep='\t') 
data 

Out[4]: 
     domain Month of date impressions clicks 
0 test1.no  jun.17  725 676 633 
1 test1.no  mai.17  422 995 456 
2 test1.no  apr.17  241 102 316 
3 test1.no  mar.17  295 157 260 
4 test1.no  feb.17  122 902 198 
5 test1.no  jan.17  137 972 201 
6 test1.no  des.16  274 435 361 
7 test2.com  jun.17 3 083 373 1 638 
8 test2.com  mai.17 3 370 620 2 036 
9 test2.com  apr.17 2 388 933 1 483 
10 test2.com  mar.17 2 410 675 1 581 
11 test2.com  feb.17 2 311 952 1 682 
12 test2.com  jan.17 1 184 787 874 
13 test2.com  des.16 2 118 594 1 738 
14 test3.com  jun.17  411 456  41 
15 test3.com  mai.17  342 048  87 
16 test3.com  apr.17  197 058 108 
17 test3.com  mar.17  288 949 156 
18 test3.com  feb.17  230 970 130 
19 test3.com  jan.17  388 032 115 
20 test3.com  des.16 1 693 442 166 
21 test4.no  jun.17  521 790 683 
22 test4.no  mai.17  438 037 541 
23 test4.no  apr.17  618 282 1 042 
24 test4.no  mar.17  576 413 956 
25 test4.no  feb.17  451 248 636 
26 test4.no  jan.17  293 217 471 
27 test4.no  des.16  641 491 978 
+0

它的工作对我来说太。谢谢!所以从看看你能明白编码是'utf-16'? – Siesta

+0

是的,如果你看维基百科页面:https://en.wikipedia.org/wiki/Byte_order_mark#Byte_order_marks_by_encoding,你会看到十六进制值和显示的字符,你习惯看到这些并在一段时间后识别它们 – EdChum