This will lead you to the right path:
import csv
import sys
file_name = sys.argv[1]
with open(file_name, 'rU') as f:
reader = csv.reader(f)
data = list(list(rec) for rec in csv.reader(f, delimiter=','))
for row in data:
print row[0]
for username in row:
print username
The last two lines will print the entire line (including "computer"). At
for x in range(1, len(row)):
print row[x]
... not to print the computer twice.
Please note that f.close () is not required when using the "with" construct, because the resource will be automatically closed when exiting the "c" block.
Personally, I would just do:
import csv
import sys
file_name = sys.argv[1]
with open(file_name, 'rU') as f:
reader = csv.reader(f)
for row in reader:
for value in row:
print value
This is a smart way to iterate over data and should give you a solid foundation for adding any extra logic.
source
share