1

I have a text file which contains float characters line by line as

    15.723
    17.567

I tried reading the file and after doing this

with open('<path_to_file>/writefile.txt', 'r') as fin:
    data_read = fin.read()

When I print data_read, I get

'15.723\n17.567\n'

Now I need to save this string into an array containg

float_array[0]=15.723
float_array[1]=17.567

How do I do the conversion?

I tried

print(float(data_read))
res = data_read.astype(float)
print(res)

And I got

ValueError: could not convert string to float: '15.723\n17.567\n'

What am I doing wrong here?

I am expecting something like float_array[0]=15.723 float_array[1]=17.567

5
  • 2
    When you say array, do you mean list? Commented Sep 9, 2022 at 13:54
  • @PM77-1 Ok... What and how should I split? The '\n' delimiter serves as saperating each variable. How do I differentiate each variable after splitting? Commented Sep 9, 2022 at 13:59
  • @MadPhysicist array list doesn't matter. As long as I can access the variable as float Commented Sep 9, 2022 at 13:59
  • @usrp_hacker. It does matter, because those two words mean different things in python. If you're not sure, you want a list. Commented Sep 9, 2022 at 14:00
  • python-reference.readthedocs.io/en/latest/docs/str/split.html Commented Sep 9, 2022 at 14:01

2 Answers 2

1

You can read the file directly into a list of strings (using readlines()) and then convert every line to float. So you don't have to use split()

with open('<path_to_file>/writefile.txt', 'r') as fin:
    data_read = fin.readlines()
numbers = list(map(float, data_read))
Sign up to request clarification or add additional context in comments.

4 Comments

ValueError: could not convert string to float: '20,807\n' I am getting such error. I am living in Europe, so the , is used as floating point here
why then you have . in you sample data and not ,? But anyway it is another problem, unrelated to your inital question.
I did data_read = data_read.replace(',', '.'). In Europe, the , is used as floating point. I wasn't sure if that is causing any issue so I did this.
It works. My bad. There was a EOL at the end of the file which was preventing the numbers from getting converted to float. It's all good now
0

First split your string into individual numbers:

list_of_strings = data_read.split()

Now you can apply float to each element:

list_of_floats = [float(x) for x in list_of_strings if x]

The check if x discards the last empty element if your file ends with a newline. If it doesn't, you can do

list_of_floats = list(map(float, list_of_strings))

Or you can strip the data first:

list_of_strings = data_read.strip().split()

4 Comments

Better be explicit here and pass the splitting parameter data_read.split('\n')
@alec_djinn. Not passing it in will automatically strip off unnecessary whitespace too, which is nice.
I tried but I am getting Traceback (most recent call last): File "<path>\PycharmProjects\pythonProject\main.py", line 17, in <module> list_of_floats = list(map(float, list_of_strings)) ValueError: could not convert string to float: ''
@usrp_hacker. Good call. Updated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.