0

I have a csv file like following

,0,1,2,3,4,5,6,7,8,9,10,11,12
0,7498.0,7499.0,,,,,,,,,,
1,7445.0,7456.0,,,,,,,,,,
2,7412.0,7413.0,7414.0,,,,,,,,,
3,7338.0,7412.0,7413.0,7414.0,,,,,,,,
4,7261.0,7021.0,,,,,,,,,,

I need to get the length of each line like following. How to do this with python?

2
2
3
4
2

This is not going to work since it counts all line splits.

f = open('myfile','r')
lines = f.readlines()

for line in lines:
    li = line.split(',')
    print len(li)
Tim
  • 41,901
  • 18
  • 127
  • 145
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121

3 Answers3

5

You apparently want to count the non-empty columns.

First, use the csv module; no need to reinvent the CSV-reading wheel. Then filter out empty columns and count:

import csv

with open('myfile', 'rb') as f:
    reader = csv.reader(f)
    for row in reader:
        # count non-empty cells, but ignore the first
        nonempty = [cell for cell in row[1:] if cell]
        print len(nonempty)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Excellant, Thank you very much :) – Nilani Algiriyage Jun 02 '14 at 12:29
  • `nonempty = sum(bool(cell) for cell in row[1:])` would be a little shorter and avoid creating a temporary list. – martineau Jun 02 '14 at 12:50
  • @martineau: but perhaps the OP wants to make use of the non-empty cells for other things as well? – Martijn Pieters Jun 02 '14 at 13:17
  • Either way the values of all the cells are still available in the original `row` list before the next one is read by the reader. If the OP wants to do additional processing of non-empty ones, that probably ought to be done with a regular `for cell in row[1:]:` loop. – martineau Jun 02 '14 at 13:44
0

You can also use the filter function:

>>> len(list(filter(bool, ['a', 'b', '', ''])))
2

More about it in another SO question here.

Community
  • 1
  • 1
0
with open("myfile") as i:
    reader = csv.reader(i)
    for line in reader:
        print(len([x for x in line if x != ""]))

Output:

13
3
3
4
5
3