If I use the following code:
with open('examplefile.csv') as tst:
for line in tst:
x = line
print(x)
I see 5 columns and numerous rows.
How do I take one row at a time and set variables to each item in a row?
If I use the following code:
with open('examplefile.csv') as tst:
for line in tst:
x = line
print(x)
I see 5 columns and numerous rows.
How do I take one row at a time and set variables to each item in a row?
The for loop already takes one line at a time
Split the line into a list.
Assuming comma is the delimiter, this assigns variables to each column
x, y, z, a, b = line.split(',')
print(x)
This also assumes you don't have nested commas within columns
For a better solution please see the csv
module
Note: Pandas has more useful functions for CSV manipulation
Use the csv module:
import csv
with open('examplefile.csv') as tst:
reader = csv.reader(tst, delimiter=',')
for line in reader:
# line is a delimiter-delimited line in your file
Hope that helps.