Reputation: 1
I want to convert specific strings to floats with the csv read file, but cant figure it out.
with open('diamonds_testing.csv') as csv_file:
csv_reader = csv.reader(csv_file)
diamond = list(csv_reader)
print(diamond[1])
print(diamond[2])
output:
['0.23', 'Ideal', 'E', 'SI2', '55', '3.95', '3.98', '2.43']
['0.31', 'Very Good', 'J', 'SI1', '62', '4.39', '4.43', '2.62']
I want to output to be floats in columns 0,4,5,6,7. Thank you.
Upvotes: 0
Views: 56
Reputation: 3005
you can try
def validate(num):
try:
return int(num)
except (ValueError, TypeError):
try:
return float(num)
except (ValueError, TypeError):
return num
with open('diamonds_testing.csv') as csv_file:
csv_reader = csv.reader(csv_file)
diamond = list(csv_reader)
for i in diamond:
diamond[i] = [validate(v) for v in diamond[i]]
Sample output
print(diamond[1])
print(diamond[2])
Output:
[0.23, 'Ideal', 'E', 'SI2', 55, 3.95, 3.98, 2.43]
[0.31, 'Very Good', 'J', 'SI1', 62, 4.39, 4.43, 2.62]
Upvotes: 0
Reputation: 149185
The csv module is a lower level module (and far less resource consuming) that the huge pandas library. As a result, it never tries to interpret the data and only split rows into strings. If you know that some columns should contain integer or floating point values, you will have to convert the values yourself.
with open('diamonds_testing.csv') as csv_file:
float_columns = (O, 4, 5, 6, 6)
csv_reader = csv.reader(csv_file)
diamond = [[float(v) if i in float_columns else v for i, v in enumerate(row)]
for row in csv_reader]
Or you can have Pandas to guess the data types:
diamond = [row[1].to_list()
for row in pd.read_csv('diamonds_testing.csv', header=None).iterrows()]
Upvotes: 0
Reputation: 339
with open('diamonds_testing.csv') as csv_file:
csv_reader = csv.reader(csv_file)
diamond = list(csv_reader)
lines = []
for line in diamond:
for i in [0, 4, 5, 6, 7]:
line[i] = float(line[i])
lines.append(line)
print(lines)
float () to do cast. to pass line by line use
for line in diamond:
Upvotes: 1