0

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)
2
  • What is the error it is producing? Commented Dec 21, 2013 at 4:11
  • @bludger the blob will come up as [] Commented Dec 21, 2013 at 4:21

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

pandas is very nice but is there a pure python way todo it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.