Issue
I want to make a code to read files that contain data from a sensor. I got stuck at the start:
import numpy as np
a = []
b = []
x = []
y = []
for line in open("YAZID.txt", "r"):
lines = [i for i in line.split()]
print(lines)
a.append(float(lines[0]))
b.append(float(lines[1]))
for i in a:
i = float(i)
x.append(i)
print(x)
it gives me this error
['0,375', '7,84E-02']
Traceback (most recent call last):
File "c:\Users\pc orange\Desktop\graphs\graph.py", line 32, in <module>
a.append(float(lines[0]))
ValueError: could not convert string to float: '0,375'
they get stuck at being strings
is there a way to make them into float so that i can make a plot with them the numbers are really little and i need to read the whole number each time
Solution
You can replace that ,
with .
and then try the same thing. Here’s the code:
for line in open("YAZID.txt", "r"):
lines = [i for i in line.split()]
print(lines)
a.append(float(lines[0].replace(",",".")))
b.append(float(lines[1].replace(",",".")))
Doing this should work for you.
And additionally at last you don’t have to make that float
again, doing just this is okay.
for i in a:
x.append(i)
print(x)
Answered By – Xitiz
Answer Checked By – Jay B. (BugsFixing Admin)