[英]problems reading a csv by column in python
I have a CSV file that has white space ie blank rows or random new lines as in the example below 我有一个CSV文件,其中包含空格,即空行或随机新行,如下例所示
header1,data1
header2,data2
header4,data4
header6,data6
The following example below works fine when the CSV has no white space, but is there a way to load a CSV by column with white space? 当CSV没有空格时,下面的示例工作正常,但有没有办法按空格加载CSV?
import csv
file = csv.reader(open('file.csv'))
blob = zip(*file)
Pandas will work: 熊猫会工作:
import pandas
pandas.read_csv("tmp.txt", header=None)
0 1
0 header1 data1
1 header2 data2
2 NaN NaN
3 header4 data4
4 NaN NaN
5 header6 data6
you probably want to filter out the NaNs. 你可能想要过滤出NaN。
I'd filter the rows before the zip
[python 2 assumed for the open
]: 我会在
zip
[python 2假设为open
]之前过滤行:
>>> import csv
>>> with open("blank.csv", "rb") as fp:
... reader = csv.reader(fp)
... rows = [line for line in reader if line]
... blob = zip(*rows)
...
>>> blob
[('header1', 'header2', 'header4', 'header6'), ('data1', 'data2', 'data4', 'data6')]
if line
here is basically equivalent to if len(line) > 0
. if line
此处的if line
基本等于if len(line) > 0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.