How to convert string values to integer values while reading a CSV file?
68,163
Solution 1
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]
Solution 2
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'])
Solution 3
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'
.
Author by
JSmooth
Updated on July 22, 2022Comments
-
JSmooth almost 2 years
When opening a CSV file, the column of integers is being converted to a string value ('1', '23', etc.). What's the best way to loop through to convert these back to integers?
import csv with open('C:/Python27/testweight.csv', 'rb') as f: reader = csv.reader(f) rows = [row for row in reader if row[1] > 's'] for row in rows: print row
CSV file below:
Account Value ABC 6 DEF 3 GHI 4 JKL 7
-
JSmooth over 8 yearsThanks, but I am getting a KeyError: 1
-
Jasper van den Berg over 8 yearsThis means there is a fault in the csv, would you mind posting a little part of the csv with the header?
-
JSmooth over 8 yearsI put it in original question
-
Jasper van den Berg over 8 yearsThe column you want to fetch needs to by typed as a string, so: integer = int(row['1']). Do not forget the quotes!!!
-
JSmooth over 8 yearsThanks, but that is giving me "IndexError: list index is out of range"
-
JSmooth over 8 yearsDo I have to specify the range, and if so, where?
-
martineau over 8 yearsSounds like some of the rows in the real .CSV file don't contain two values. If that's the case, you could skip them by changing the end of the list comprehension to
...for row in reader if len(row) > 1]
.