采集卡数据文件:
CH2 长度:359775点
----------------------------------
X轴单位:s Y轴单位:V
3.198E-003 -4.2724609375E-004
3.19802E-003 -1.15966796875E-003
3.19804E-003 -6.7138671875E-004
3.19806E-003 -1.3427734375E-003
3.19808E-003 -7.9345703125E-004
需要读两列画图。
下面是解决方案:
import matplotlib.pyplot as plt
X, Y = [], []
with open('file.txt', 'r') as f:
lines = f.readlines()
lines = lines[3:]
for line in lines:
values = [float(s) for s in line.split()]
X.append(values[0])
Y.append(values[1])
plt.plot(X, Y) #二维数据是折线图,一维数据就是柱状图
plt.show()
参考的链接如何在Python中删除.txt文件的前几行? - 问答 - Python中文网
下面是评论区的解决方法
删除注释行或参数单位行
此函数用于删除所有注释行
def remove_comments(lines):
return [line for line in lines if line.startswith("#") == False]
删除n个顶行
def remove_n_lines_from_top(lines, n):
if n <= len(lines):
return lines[n:]
else:
return lines
以下是完整的来源:
with open('file.txt') as f:
lines = f.readlines()
def remove_comments(lines):
return [line for line in lines if line.startswith("#") == False]
def remove_n_line(lines, n):
return lines[n if n<= len(lines) else 0:]
lines = remove_n_lines_from_top(lines, 3)
f = open("new_file.txt", "w+") # save on new_file
f.writelines(lines)
f.close()