Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading a text file and converting string to float

I have a text file called "foo.txt", with a list of numbers, one on each line, for example:

0.094195
0.216867
0.326396
0.525739
0.592552
0.600219
0.637459
0.642935
0.662651
0.657174
0.683461

I now want to read these numbers into a Python list. My code to do this is as follows:

x = []
file_in = open('foo.dat', 'r')
for y in file_in.read().split('\n'):
    x.append(float(y))

But this gives me the error:

ValueError: could not convert string to float

What am I doing wrong?

like image 798
Karnivaurus Avatar asked Sep 10 '26 09:09

Karnivaurus


2 Answers

Edit:

commented by martineau: you can also use if y: to eliminate None or empty string.

Original Answer:

It fails due to you are using newline character as a separator, therefore the last element is empty string

you can add y.isdigit() to check whether y is numeric.

x = []
file_in = open('sample.csv', 'r')
for y in file_in.read().split('\n'):
    if y.isdigit():
        x.append(float(y))

OR

you can change read().split("\n") to readlines()

OR

remove the leading/trailing characters from y. it handles the lines with extra whitespaces

for y in file_in:
    trimed_line = y.strip()  # leading or trailing characters are removed
like image 123
Haifeng Zhang Avatar answered Sep 12 '26 05:09

Haifeng Zhang


How about this approach:

x = []
with open('foo.dat', 'r') as f:
    for line in f:
        if line: #avoid blank lines
            x.append(float(line.strip()))

Or:

with open('foo.dat', 'r') as f:
    lines = (line.strip() for line in f if line)
    x = [float(line) for line in lines]

Finally more compact:

with open('foo.dat', 'r') as f:
    x = [float(line.strip()) for line in f if line]

This way you don't have to worry about blank lines and you make proper conversion from string to float

like image 34
Iron Fist Avatar answered Sep 12 '26 07:09

Iron Fist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!