numpy.loadtxt()中的转换错误 [英] Conversion error in numpy.loadtxt()

查看:169
本文介绍了numpy.loadtxt()中的转换错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python matplotlib从文本文件中绘制两列,但是我得到了

I am trying to plot two columns from a text file using python matplotlib but I am getting

ValueError:无效的float()文字:148.000000;

ValueError: invalid literal for float(): 148.000000;

这是我的python脚本

This is my python script

import numpy as np
import matplotlib.pyplot as plt
x,y = np.loadtxt('sharma5.txt')
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y)
plt.show()

这是我的文本文件的一部分

and here is a part of my text file

36.000000   61.000000
36.000000   61.000000
36.000000   148.000000;
36.000000   60.000000
36.000000   120.000000
36.000000   77.000000
36.000000   160.000000

先谢谢了.

推荐答案

如果不想修复数据文件,可以对loadtxt使用converters选项以删除所有多余的分号. np.loadtxt("sharma5.txt", converters = {1: lambda s: float(s.strip(";"))})之类的东西应该可以工作.

In case you don't want to fix your data file, you can use the converters option to loadtxt in order to remove any extraneous semicolons. Something like np.loadtxt("sharma5.txt", converters = {1: lambda s: float(s.strip(";"))}) should work.

这篇关于numpy.loadtxt()中的转换错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆