I have some DSC data which i want to plot but i have a problem converting strings to floats, I have a .txt file that looks like this:

42 views Asked by At
     Index             t      Heatflow            Tr
       [#]             [s]             [mW]          [°C]
         0   0,00000e+00   9,39705e-02  -7,00000e+01
         1   1,00000e+00   9,01634e-02  -7,00000e+01
         2   2,00000e+00   8,46441e-02  -7,00000e+01
         3   3,00000e+00   7,92335e-02  -7,00000e+01

When using

filename = 'HA_A001_PBS.txt'
data = np.loadtxt(filename, skiprows = 2)
Temp = data[:,3]
HeatFlow = data[:,2]

the ValueError: could not convert string '1,00000e+00' to float64 at row 0, column 2 occurs

How do I convert these types of strings to floats to plot this type of data?

1

There are 1 answers

3
Andrej Kesely On

Try to use converters= parameter:

filename = "your_file.txt"
data = np.loadtxt(
    filename, skiprows=2, encoding="utf-8", converters=lambda s: s.replace(",", ".")
)
print(data)

Prints:

[[  0.          0.          0.0939705 -70.       ]
 [  1.          1.          0.0901634 -70.       ]
 [  2.          2.          0.0846441 -70.       ]
 [  3.          3.          0.0792335 -70.       ]]