lisa
lisa

Reputation: 71

converting a list with strings into a list with floats

import csv
with open ('data.txt', 'r') as f:
    col_one = [row[0] for row in csv.reader(f, delimiter= '\t')]
    plots = col_one[1:]

The data in column one are floats, but above code makes list of strings. How can I make the list of floats correcting above codes?

Upvotes: 0

Views: 221

Answers (2)

karthikr
karthikr

Reputation: 99620

You can convert string to float using float() function

import csv
with open ('data.txt', 'r') as f:
    col_one = [float(row[0]) for index, row in enumerate(csv.reader(f, delimiter= '\t')) if index != 0]

Upvotes: 1

thomasd
thomasd

Reputation: 2612

Use the float built-in:

col = [float(row[0]) for row in rows]

http://docs.python.org/dev/library/functions.html#float

Upvotes: 1

Related Questions