pyCthon
pyCthon

Reputation: 12361

problems reading a csv by column in python

I have a CSV file that has white space i.e. blank rows or random new lines as in the example below

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?

import csv

file = csv.reader(open('file.csv'))
blob = zip(*file)

Upvotes: 0

Views: 73

Answers (2)

DSM
DSM

Reputation: 353379

I'd filter the rows before the zip [python 2 assumed for the 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.

Upvotes: 1

James King
James King

Reputation: 6365

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.

Upvotes: 1

Related Questions