Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
import csv

ifile=csv.reader(open('C:\Users\BKA4ABT\Desktop\Test_Specification\RDBI.csv','rb'),delimiter=';')

for line in ifile:

    row = line
    if [True for cell in row if cell=='']: 
        continue


    print row[1]
    print row[2]
    print row[3]

Here I have used row[1] and row[2], but I want to use just one time like row [i].

share|improve this question

closed as off-topic by Aseem Bansal, palacsint, Lstor, Vedran Šego, Brian Reichle Sep 20 '13 at 14:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Aseem Bansal, palacsint, Lstor, Vedran Šego, Brian Reichle
If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 1 down vote accepted

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
share|improve this answer
    
Thank you tobias for your eeply, i have tried with your code also but i want to access individual cell and if cell is empty then ii must be skipped. –  ketan Sep 20 '13 at 13:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.