So I am doing a starters course in Python, and I need to make the following: There is a CSV file, where there are 10 columns, filled with 200 rows. Each has either a str
, int
, or float
as value.
Example input:
id gender age marital location income intelliscore emotiscore
51 F 46 M 0 15100 531 555
52 M 29 M 2 14200 673 633
53 M 25 S 0 22200 742 998
54 M 36 M 2 1000 677 646
55 F 99 S 0 10600 608 998
Now what i gotta do, is create another CSV file, and "replace" these values, with the types. So the desired result would be:
'string', 'string', 'string', 'string', 'string', 'string', 'string', 'string'
'int', 'string', 'int', 'string', 'int', 'int', 'int', 'int', 'int'
'int', 'string', 'int', 'string', 'int', 'int', 'int', 'int', 'int'
'int', 'string', 'int', 'string', 'int', 'int', 'int', 'int', 'int'
'int', 'string', 'int', 'string', 'int', 'int', 'int', 'int', 'int'
The code I am currently using is:
def csvfields2types(self, csvfile):
csvtypes = []
for line in csvfile:
row = []
for variable in line:
if variable == str:
row.append('string')
elif variable == float:
row.apend('float')
elif variable == int:
row.append('int')
else:
row.append('huh?')
csvtypes.append(row)
return csvtypes
It just returns a list with 'huh?'.