python - 用python查找第一行数据
问题描述
我有一个看起来像这样的数据文件,文件类型是一个列表。
############################################################
# Tool
# File: test
#
# mass: mass in GeV
# spectrum: from 1 to 100 GeV
###########################################################
# mass (GeV) spectrum (1-100 GeV)
10 0.2822771608053263
20 0.8697454394829301
30 1.430461657476815
40 1.9349004472432392
50 2.3876849629827412
60 2.796620869276766
70 3.1726347734996727
80 3.5235401505002244
90 3.8513847250834106
100 4.157478780924807
为了读取数据,我通常必须计算第一组数字之前的行数,然后 for 循环遍历文件。在这个文件中它的 8 行
spectrum=[]
mass=[]
with open ('test.in') as m:
test=m.readlines()
for i in range(8,len(test)):
single_line=test[i].split('\t')
mass.appened(float(single_line[0]))
spectrum.append(float(single_line[1]))
假设我不想打开文件来检查从 intro 语句到第一行数据点的行数。如何让 python 自动从第一行数据点开始,并遍历文件的末尾?
解决方案
spectrum=[]
mass=[]
with open ('test.in') as m:
test=m.readlines()
for line in test:
if line[0] == '#':
continue
single_line=line.split('\t')
mass.append(float(single_line[0]))
spectrum.append(float(single_line[1]))
推荐阅读
- jqgrid - 使用 optgroup 时 select2 不会自动选择
- javascript - 根据所选选项动态使用不同的数据对象
- datepicker - datepicker 日期格式小问题
- node.js - 如何根据 Nodejs 中的 LUIS 识别意图从对话框中实现瀑布对话框?
- java - 如何根据 Html 标签拆分 Html 字符串
- yaml - docker compose 中多个服务的 yaml 文件配置
- amcharts - Amcharts,是否可以在将 ValueAxis 用于 x 轴时设置系列工具提示
- azure - 如果从 .NET SDK 服务主体帐户创建,则无法在 Azure 门户中看到实时流媒体频道
- python - 如何从.exe文件中删除用python编写的.exe文件?
- javascript - 函数参数内的箭头函数缩小不正确