I'm new in python language and I'm trying to handle a csv file with this very simple test code:
import csv
csvfile= str(input("write file input name\n"))
read_file = open(csvfile)
file_in = csv.reader(read_file, delimiter=' ')
print("number of rows of",csvfile,"is:",len(list(file_in)))
i=1
for var in file_in :
if i>1:
print(var[1])
i +=1
print("number of rows of",csvfile,"is:",len(list(file_in)))
giving this code a simple file "letters.csv" containing 7 rows like this:
a 2
b 7
c 9
d 11
e 3
f 9
g 96
what's strange for me is:
- why this code enter the for-loop and returns what I want only if I comment line 5 (print("number...)? Uncommenting line 5 the output is:
write file input name
letters.csv
number of rows of letters.csv is: 7
number of rows of letters.csv is: 0
commenting line 5 the output is:
write file input name
letters.csv
7
9
11
3
9
96
number of rows of letters.csv is: 0
- Why the expression in the last line (equal to line 5) returns always 0? Shouldn't be 7?
csvreader by converting it to alist.