python error : ValueError: could not convert string to float: (maybe system env related)

python error : ValueError: could not convert string to float: (maybe system env related)

我有简单的 python 代码来读取数据文件和绘图。 它在以前的系统中运行良好。我的系统崩溃了,我设置了另一个更高 ubuntu 版本的系统。 当我 运行 脚本时,我遇到了以前系统中不存在的错误。 之前的系统坏了,无法详细比较

代码

 import matplotlib.pyplot as plt
 import csv
 import sys
 def analyze():

     datafile = sys.argv[1]
     pieces = []
     with open(datafile, 'rt') as f:
          data = csv.reader(f,delimiter = '\t')
          for d in data:
            pieces.append(d)

    x = [op for op, response, interval in pieces]
    #y1 = [response for op, response, interval in pieces]
    y1 = [interval for op, response, interval in pieces]

    p1 = plt.plot (x, y1, label='test')
    plt.legend()
    plt.title ('AAAAA test')
    plt.xlabel('# of Operation'), plt.ylabel('Response time')

    #plt.hist(y1, 100)
    plt.show()

if __name__ == '__main__':
    analyze()

数据文件

    0             277.94                0.00
     1            277.94             6553.00
     2            277.94            32768.00
     3            277.94            32768.00
     4            277.94            45875.00
     5            277.94            13108.00
     6            277.94            26215.00
     7            232.93             6553.00
     8            277.94            13107.00
     9            232.93            39321.00
    10            301.94             6554.00
    11            232.93             6554.00
    12            277.94            13108.00
    13            232.93            19661.00
    14            236.93             6554.00
    15            285.94            19661.00
    16            232.93            32768.00
    17            277.94             6554.00
    continue......

错误 (Python 2.7)

 Traceback (most recent call last):
   File "../scripts/plot_interval.py", line 27, in <module>
     analyze()
   File "../scripts/plot_interval.py", line 18, in analyze
     p1 = plt.plot (x, y1, label='test')
   File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3154,  in plot
     ret = ax.plot(*args, **kwargs)
   File "/usr/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1814, in inner
     return func(ax, *args, **kwargs)
   File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 1425, in plot
     self.add_line(line)
   File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1708, in add_line
     self._update_line_limits(line)
   File "/usr/lib/python2.7/dist-packages/matplotlib/axes/_base.py", line 1730, in _update_line_limits
     path = line.get_path()
   File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 925, in get_path
     self.recache()
   File "/usr/lib/python2.7/dist-packages/matplotlib/lines.py", line 612, in recache
     x = np.asarray(xconv, np.float_)
   File "/usr/lib/python2.7/dist-packages/numpy/core/numeric.py", line 482, in asarray
     return array(a, dtype, copy=False, order=order)
 ValueError: could not convert string to float: ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒

错误 (python3)

 Traceback (most recent call last):
   File "../scripts/plot_interval.py", line 27, in <module>
     analyze()
   File "../scripts/plot_interval.py", line 11, in analyze
     for d in data:
   File "/usr/lib/python3.5/codecs.py", line 321, in decode
     (result, consumed) = self._buffer_decode(data, self.errors, final)
 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe6 in position 0:  invalid continuation byte

我可以建议一个非常简单的调试步骤:

在返回数组之前,打印 var a 并查看是否存在不表示有效浮点数的字符串。

当您找到它们时,您需要知道它们为什么在那里。