How do I fix this error: "ValueError: could not convert string to float: '-1,89E-09'"?

How do I fix this error: "ValueError: could not convert string to float: '-1,89E-09'"?

我想通过读取这些数字并将这些数字放入 y 列表来以科学记数法绘制数字。这是我的代码。

import matplotlib.pyplot as plt
import csv

x = []
y = []


with open('test1.txt','r') as csvfile:
     plots = csv.reader(csvfile, delimiter='\t')  
        for row in plots:
        x.append(float(row[0]))
        y.append(float(row[1]))


  plt.plot(x,y, label='20 Volt max with filter')
  plt.xlabel('time')
  plt.ylabel('voltage')
  plt.show()

.txt 文件如下所示:

0   -1,89E-09
0,001   -1,37E-08
0,002   -5,69E-08

错误是:

ValueError: could not convert string to float: '-1,89E-09'

在 Python 和许多其他语言中,标准的小数点分隔符是 .(句点)而不是 ,(逗号)。

来自维基百科上的Decimal Mark

In 1958, disputes between European and American ISO delegates over the correct representation of the decimal mark nearly stalled the development of the ALGOL computer programming language. ALGOL ended up allowing different decimal marks, but most computer languages and standard data formats (e.g. C, Java, Fortran, Cascading Style Sheets (CSS)) specify a dot.

您可以更改文本文件中使用的格式,或者您可以 float(row[n].replace(",", "."))