Not sure whether I understood that bit about row[i]
correctly, but here's my version of the code:
with open('data','rb') as f:
for row in csv.reader(f, delimiter=';'):
if not all(row): continue
for cell in row:
print cell
Some notes:
- You should use the
with
statement for opening files; this will automatically close the file once you are done. In your code, you do not even keep a handle to the file.
- Your
if
statement is a bit hard to comprehend. You can just use all(row)
to check whether all the cells are non-empty, or a bit more verbose: any(cell == '' for cell in row)
.
- Finally -- if I understood the question right -- you can just loop over the cells in the row. Alternatively, you can also use
for i in range(1, 4): print row[i]
to print just the indices 1 - 3.
Update: If you want to skip individual empty cells, instead of entire rows with empty cells. you could use the following:
for row in csv.reader(f, delimiter=';'):
for cell in row:
if cell != '':
print cell