Python 打印.txt文件时,无法将字符串转换为浮点
我正在尝试绘制一个.txt文件,其中的行的格式为:filename.txt、date、magnificate Ex:V098550.txt,362.0,3.34717962317 我正试图根据震级来划分日期 我不熟悉编码和获取信息: ValueError:无法将字符串转换为浮点:V113573.txt、362.0、3.5425960309 你知道我怎样才能解决这个问题吗Python 打印.txt文件时,无法将字符串转换为浮点,python,numpy,matplotlib,plot,Python,Numpy,Matplotlib,Plot,我正在尝试绘制一个.txt文件,其中的行的格式为:filename.txt、date、magnificate Ex:V098550.txt,362.0,3.34717962317 我正试图根据震级来划分日期 我不熟悉编码和获取信息: ValueError:无法将字符串转换为浮点:V113573.txt、362.0、3.5425960309 你知道我怎样才能解决这个问题吗 import numpy as np import matplotlib.pyplot as plt names = '/h
import numpy as np
import matplotlib.pyplot as plt
names = '/home/sindelj/research/condensed.txt'
for ii in range (len(names)):
lc = np.loadtxt ("condensed.txt")
plt.scatter (lc[:,0],lc[:,1])
plt.xlabel ('Time')
#take mean date
#date = []
#date_all = numpy.mean(date)
#plt.xlim ([date_all+1, date_all-1])
plt.ylabel ('Mag')
plt.ylim ([15.,14.])
plt.show()# after test comment this out
fileName = names[ii][:-3] + ".png"
plt.savefig(fileName)
print "done"
根据文档,您可以使用usecols
参数指定要加载的列。另外,unpack
参数允许按列返回数据
import numpy as np
import matplotlib.pyplot as plt
names = '/home/sindelj/research/condensed.txt'
for ii in range (len(names)):
x, y = np.loadtxt ("condensed.txt", usecols=(1, 2), unpack=True)
plt.scatter (x, y)
plt.xlabel ('Time')
plt.ylabel ('Mag')
plt.ylim ([15.,14.])
plt.show() # after test comment this out
fileName = names[ii][:-3] + ".png"
plt.savefig(fileName)
print "done"
您可以使用
np.loadtxt(“condensed.txt”,usecols=(1,2))
@user128285跳过第一列。您是否可以提供您的评论作为答案,并且可以接受?