【问题标题】:Looking for first line of data with python用python查找第一行数据
【发布时间】:2020-10-13 02:57:15
【问题描述】:

我有一个像这样的数据文件,文件类型是一个列表。

############################################################
# 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 自动从第一行数据点开始,并遍历文件末尾?

【问题讨论】:

标签: python python-3.x


【解决方案1】:

伪代码:

for r in m:
    if r.startwith('#'):
        continue
    spt = r.split('\t')
    if len(spt) < 2:
        continue
    ## todo: .....

【讨论】:

    【解决方案2】:

    您可以通过正则表达式或字符串的startswith方法过滤所有以#开头的行

    import re
    
    spectrum=[]
    mass=[]
    with open ('test.in') as m:
        test= [i for i in f.readlines() if not re.match("^#.*", i)]
        for i in test:
            single_line = i.split('\t')
            mass.appened(float(single_line[0]))
            spectrum.append(float(single_line[1]))
    

    spectrum = []
    mass = []
    with open('test.in') as m:
        test = [i for i in f.readlines() if not i.startwith("#")]
        for i in test:
            single_line = i.split('\t')
            mass.appened(float(single_line[0]))
            spectrum.append(float(single_line[1]))
    

    这将过滤掉所有以#开头的行。

    【讨论】:

    • 你可以使用i.startswith('#')代替正则表达式
    • Tnx 添加到答案中。
    【解决方案3】:

    这是一个通用解决方案,但应该适用于您的具体情况。

    你可以为每一行检查它是否以数字开头。

    伪代码

    for line in test:
        if line.split()[0].isdigit():
            DoStuffWithData
    

    【讨论】:

    • 这很好用!但是 print(line) 是一个字符串,我怎么能把它变成一个列表?
    • 我不明白你的意思,但我们可以将一个字符串做成这样的列表:the_string.split(“”)
    【解决方案4】:
    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]))
    

    【讨论】:

    • 你可以使用startswith(),而不是检查相等检查不相等然后追加数据
    猜你喜欢
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-22
    • 2021-02-28
    • 1970-01-01
    相关资源
    最近更新 更多