How to convert string values to integer values while reading a CSV file?
I think this does what you want:
import csv
with open('C:/Python27/testweight.csv', 'r', newline='') as f:
reader = csv.reader(f, delimiter='\t')
header = next(reader)
rows = [header] + [[row[0], int(row[1])] for row in reader if row]
for row in rows:
print(row)
Output:
['Account', 'Value']
['ABC', 6]
['DEF', 3]
['GHI', 4]
['JKL', 7]
If the CSV has headers, I would suggest using csv.DictReader
. With this you can do:
with open('C:/Python27/testweight.csv', 'rb') as f:
reader = csv.DictReader(f)
for row in reader:
integer = int(row['Name of Column'])
You could just iterate over all of the rows as follows:
import csv
with open('testweight.csv', newline='') as f:
rows = list(csv.reader(f)) # Read all rows into a list
for row in rows[1:]: # Skip the header row and convert first values to integers
row[1] = int(row[1])
print(rows)
This would display:
[['Account', 'Value'], ['ABC', 6], ['DEF', 3], ['GHI', 4], ['JKL', 7]]
Note: your code is checking for > 's'
. This would result in you not getting any rows as numbers would be seen as less than s
. If you still use Python 2.x, change the newline=''
to 'rb'
.