How do I tell map()
to selectively convert only some of the strings (not all the strings) within a list to integer values?
Input file (tab-delimited):
abc1 34 56
abc1 78 90
My attempt:
import csv
with open('file.txt') as f:
start = csv.reader(f, delimiter='\t')
for row in start:
X = map(int, row)
print X
Error message: ValueError: invalid literal for int() with base 10: 'abc1'
When I read in the file with the csv
module, it is a list of strings:
['abc1', '34', '56']
['abc1', '78', '90']
map()
obviously does not like 'abc1'
even though it is a string just like '34'
is a string.
I thoroughly examined Convert string to integer using map() but it did not help me deal with the first column of my input file.