如何在Pandas中读取奇怪的csv文件?

时间:2016-09-13 05:49:59

标签: python csv pandas

我想阅读下面显示的示例csv文件

--------------
 |A|B|C| 
--------------
 |1|2|3| 
--------------
 |4|5|6| 
--------------
 |7|8|9| 
--------------

我试过

pd.read_csv("sample.csv",sep="|")

但它效果不佳。

我怎么读这个csv?

3 个答案:

答案 0 :(得分:11)

您可以将参数comment添加到read_csv,然后按dropna删除NaN列:

import pandas as pd
import io

temp=u"""--------------
|A|B|C|
--------------
|1|2|3|
--------------
|4|5|6|
--------------
|7|8|9|
--------------"""
#after testing replace io.StringIO(temp) to filename
df = pd.read_csv(io.StringIO(temp), sep="|", comment='-').dropna(axis=1, how='all')

print (df)
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9

更一般的解决方案:

import pandas as pd
import io

temp=u"""--------------
|A|B|C|
--------------
|1|2|3|
--------------
|4|5|6|
--------------
|7|8|9|
--------------"""
#after testing replace io.StringIO(temp) to filename
#separator is char which is NOT in csv
df = pd.read_csv(io.StringIO(temp), sep="^", comment='-')

#remove first and last | in data and in column names
df.iloc[:,0] = df.iloc[:,0].str.strip('|') 
df.columns = df.columns.str.strip('|')
#split column names
cols = df.columns.str.split('|')[0]
#split data
df = df.iloc[:,0].str.split('|', expand=True)
df.columns = cols
print (df)
   A  B  C
0  1  2  3
1  4  5  6
2  7  8  9

答案 1 :(得分:1)

尝试“导入csv”而不是直接使用pandas。

import csv

easy_csv = []

with open('sample.csv', 'rb') as csvfile:
   test = csv.reader(csvfile, delimiter=' ', quotechar='|')
   for row in test:
      row_preprocessed = """ handling rows at here; removing |, ignoring row that has ----"""
      easy_csv.append([row_preprocessed])

完成此预处理后,您可以将其保存为逗号分隔的csv文件,以便轻松处理pandas。

答案 2 :(得分:0)

我试试这个代码并且没问题!:

import pandas as pd
import numpy as np
a = pd.read_csv("a.csv",sep="|")
print(a)
for i in a:
    print(i)

enter image description here