我对Python和编程都是新手。 我试图读取两行(x和y)中包含十进制数据的文件,但出现以下错误:“无法将字符串转换为浮点:'#'”。我如何解决这个问题?你知道吗
with open('file.txt','r') as csvfile:
... plots=csv.reader(csvfile, delimiter=' ')
... for row in plots:
... x.append(int(float(row[0])))
... y.append(int(float(row[1])))
...
Traceback (most recent call last):
File "", line 4, in
ValueError: could not convert string to float: '#'
编辑:上半部分已经解决了(感谢大家的快速回复!)。问题是文件有一个标题行,我删除了它。当然,可以从2号开始打开文件。而且文件的列之间有两个空格而不是一个,所以我用记事本替换了字符,因为Python只允许一个字符长的定界符。你知道吗
现在我试着画它,我得到另一个错误‘x和y必须有相同的第一维度,但有形状’。代码中潜在的错误是什么? 完整代码:
import matplotlib.pyplot as plt
import csv
x=[]
y=[]
with open('file.txt','r') as csvfile:
... plots=csv.reader(csvfile, delimiter=' ')
... for row in plots:
... x.append(int(float(row[0])))
... y.append(int(float(row[1])))
...
plt.plot(x,y, label='spectra')
ValueError: x and y must have same first dimension, but have shapes (218605,) and (218604,)
该文件有200000行,前5行是:
4066.46745678 0.794766
4066.49045678 0.979478
4066.51345678 0.837846
4066.53645678 0.935674
4066.55945678 0.91354
目前没有回答
相关问题 更多 >
编程相关推荐