7

I have two text files composed of spaced-separated columns. These are excerpts of these two files:

FileA

 1 1742.420   -0.410  20.1530   0.4190   1.7080   0.5940
 2 1872.060    0.070  21.4710   0.2950   0.0670   0.3380
 3 1918.150    0.150  18.9220   0.0490   1.4240   0.1150
 4 1265.760    0.170  19.0850   0.0720   1.3330   0.1450
 5  308.880    0.220  20.5020   0.1570   0.0200   0.1720
 ....

FileB

 1 1198.367    6.465  15.684 0.015  3.119 0.140  1
 2 1451.023    6.722  17.896 0.031  0.171 0.041  1
 3 1032.364    6.788  18.895 0.074 -0.084 0.088  1
 4  984.509    7.342  19.938 0.171  0.043 0.322  1
 5 1068.536    7.369  19.182 0.091  0.486 0.143  1
 ....

As you can see FileA has 7 columns and FileB has 8. This is the code I use to count the columns:

import csv
file = 'filename'
reader = csv.reader(file)
num_cols = 0
for col in reader:
    num_cols = num_cols + 1

This little code correctly gives the number of columns for FileA (7) but not for FileB (also gives 7) What's going on and how can I fix it?

If I'm counting rows instead of columns then: 1- how can I count the columns? and 2- given that my actual files have several thousands of lines/rows, why am I getting these results (7)?

3
  • 3
    You are counting rows. Commented May 8, 2013 at 19:43
  • The actual text files have several thousands lines/rows, so if I'm counting rows that raises another set of questions. Commented May 8, 2013 at 19:44
  • @MartijnPieters is correct. You could say for bunnies in reader and you will still be looking at rows, not columns. Just count the elements of the first row, as others have suggested. Commented May 8, 2013 at 19:53

3 Answers 3

14
import csv

with open('filename') as f:
    reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
    first_row = next(reader)
    num_cols = len(first_row)
Sign up to request clarification or add additional context in comments.

6 Comments

No need to use iter() here; csv.reader() objects are iterable directly.
@MartijnPieters: I wasn't sure. I extrapolated from next([1, 2, 3]) erroring. You're right though - next(csv.reader(['a, b, c'])) works just fine
The code works great but could you explain a bit more what is it doing? I'm having trouble understanding the with and the next statements in particular.
@Gabriel: the with statement is the prefered way to work with files - it closes the file automatically when the scope is left. next takes an iterator, and returns the next (in this case, first) item from it. next(my_iterator) is equivalent to list(my_iterator)[0]
@Gabriel: except list(it) iterates over all values.
|
2

Read just the first line, use split to find all non-whitespace parts, count those:

with open("bla") as f:
    line = f.readline()
print(len(line.split()), "columns")

Comments

0

just get the length of the first row!!

import csv
file = 'my_File.csv'
reader = csv.reader(open(file,'rb'),delimiter=" ")
num_cols = len(next(read))

3 Comments

I don't think csv.reader takes a filename. Also, this is not normal csv, so requires extra configuration to parse.
Also, python 3 deprecates/removes i.next() in favor of next(i)
I had no clue as to I use 2.7. And yes That was a typo, I forgot to add the open

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.