import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\sampledata.csv"
with open(filename, newline='') as file:
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
wv2 = float(row[2])
data.append([measured_time, wv1,wv2])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
工作得很好,但因为这是在我处理真实数据之前进行的测试。当specyfing定界符时导入csv数据行和列
现在真实数据没有标题,只包含2列。
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\real_data.csv"
with open(filename, newline='') as file:
reader = csv.reader(file,delimiter=';')
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
data.append([measured_time, wv1])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
这产生
时间= [100.1]
Y1 = [110,2]
所以基本上只是第2行,而第一码块正确地提取的列
时间= [100 110 ...]
Y1 = [1 2 ...]
同样的事情发生时,我使用
import csv
import matplotlib.pyplot as plt
filename = r"C:\Users\name\Desktop\sampledata.csv"
with open(filename, newline='') as file:
reader = csv.reader(file)
header = next(reader)
data = []
for row in reader:
measured_time = float(row[0])
wv1 = float(row[1])
data.append([measured_time, wv1])
time=data[0]
y1=data[1]
plt.plot(time,y1)
plt.show()
现在我的解决办法是
data = np.array(data).astype("float")
time=data[:,0]
y1=data[:,1]
当然,必须有一个更好的办法,我刚开始使用Python。
数据使用:
sample data
Time,279 nm,306 nm
0,100,150
1,110,175
2,125,230
3,130,245
real data
3.986931e+002;0.000000e+000
4.021733e+002;0.000000e+000
是';'在文件中使用了分隔符吗?你确定你没有错误的数据文件? – Laszlowaty
你可以添加几行从真实文件到帖子 – Marco
@Laszlowaty是的;是真实数据中的分隔符,我会排除错误,因为我提到的解决方法是有效的。 @ Marco完成 – idkfa