Python 读取.txt文件中的数据,不包括页眉和页脚

Python 读取.txt文件中的数据,不包括页眉和页脚,python,python-2.7,file-io,Python,Python 2.7,File Io,我有一个.txt文件,看起来像: abcd this is the header more header, nothing here I need *********** column1 column2 ========= ========= 12.4 A 34.6 mm 1.3 um ===================== footer, nothing that I need here ***** more text ******

我有一个.txt文件,看起来像:

abcd this is the header
more header, nothing here I need
***********
column1    column2
=========  =========
  12.4       A
  34.6       mm
  1.3        um
=====================
footer, nothing that I need here
***** more text ******
我试图读取列中的数据,每个列都进入它自己的列表,col1=[12.4,34.6,1.3]和col2=['A','mm','um']

这是到目前为止我所拥有的,但是当我运行代码时返回的唯一内容是“无”:

def readfile():
    y = sys.argv[1]

    z = open(y)
    for line in z:

        data = False
        if data == True:
            toks = line.split()
            print toks

        if line.startswith('=========  ========='):
            data = True
            continue

        if line.startswith('====================='):
            data = False
            break
print readfile()

有什么建议吗

您采用的方法可能效率不高,但有点问题&因此您的数据提取错误

您需要在
行之后立即触发boolen,即
数据
。startswith('==================================')
&因此,在此之前,应将其保持为
False

在上面,您的数据将被提取,直到
行.startswith('======================')

希望我没弄错

def readfile():
    y = sys.argv[1]
    toks = []
    with open(y) as z:
        data = False

        for line in z:

            if line.startswith('=========  ========='):
                data = True
                continue

            if line.startswith('====================='):
                data = False
                break

            if data:
                toks.append(line.split())
                print toks
    col1, col2 = zip(*toks) # Or just simply, return zip(*toks)
    return col1, col2

print readfile()

with
语句比
z=open(file)
更具python风格&您采用的方法可能不是很有效,但有点错误&因此您的数据提取错误

您需要在
行之后立即触发boolen,即
数据
。startswith('==================================')
&因此,在此之前,应将其保持为
False

在上面,您的数据将被提取,直到
行.startswith('======================')

希望我没弄错

def readfile():
    y = sys.argv[1]
    toks = []
    with open(y) as z:
        data = False

        for line in z:

            if line.startswith('=========  ========='):
                data = True
                continue

            if line.startswith('====================='):
                data = False
                break

            if data:
                toks.append(line.split())
                print toks
    col1, col2 = zip(*toks) # Or just simply, return zip(*toks)
    return col1, col2

print readfile()

with
语句比
z=open(file)
更具python风格&有很多方法可以做到这一点

一种方法涉及:

  • 将文件读入行中
  • 从读取的行中,找到包含列标题分隔符的行的索引(因为这也与页脚标题匹配)
  • 然后,在这些行之间存储数据
  • 通过基于空格拆分这些行并将它们存储到各自的列中来解析这些行
  • 像这样:

    with open('data.dat', 'r') as f:
        lines = f.readlines()
    
        #This gets the limits of the lines that contain the header / footer delimiters
        #We can use the Column header delimiters double-time as the footer delimiter:
        #`=====================` also matches against this.
        #Note, the output size is supposed to be 2. If there are lines than contain this delimiter, you'll get problems
        limits = [idx for idx, data in enumerate(lines) if '=========' in data]
    
        #`data` now contains all the lines between these limits
        data = lines[limits[0]+1:limits[1]] 
    
        #Now, you can parse the lines into rows by splitting the line on whitespace
        rows = [line.split() for line in data]
    
        #Column 1 has float data, so we convert the string data to float
        col1 = [float(row[0]) for row in rows]
    
        #Column 2 is String data, so there is nothing further to do
        col2 = [row[1] for row in rows]
    
        print col1, col2
    
    这将输出(来自您的示例):


    有很多方法可以做到这一点

    一种方法涉及:

  • 将文件读入行中
  • 从读取的行中,找到包含列标题分隔符的行的索引(因为这也与页脚标题匹配)
  • 然后,在这些行之间存储数据
  • 通过基于空格拆分这些行并将它们存储到各自的列中来解析这些行
  • 像这样:

    with open('data.dat', 'r') as f:
        lines = f.readlines()
    
        #This gets the limits of the lines that contain the header / footer delimiters
        #We can use the Column header delimiters double-time as the footer delimiter:
        #`=====================` also matches against this.
        #Note, the output size is supposed to be 2. If there are lines than contain this delimiter, you'll get problems
        limits = [idx for idx, data in enumerate(lines) if '=========' in data]
    
        #`data` now contains all the lines between these limits
        data = lines[limits[0]+1:limits[1]] 
    
        #Now, you can parse the lines into rows by splitting the line on whitespace
        rows = [line.split() for line in data]
    
        #Column 1 has float data, so we convert the string data to float
        col1 = [float(row[0]) for row in rows]
    
        #Column 2 is String data, so there is nothing further to do
        col2 = [row[1] for row in rows]
    
        print col1, col2
    
    这将输出(来自您的示例):


    如果知道文件的页眉/页脚行数,则可以使用此方法

    path = r'path\to\file.csv'
    header = 2
    footer = 2
    buffer = []
    
    with open(path, 'r') as f:
        for _ in range(header):
            f.readline()
    
        for _ in range(footer):
            buffer.append(f.readline())
    
        for line in f:
            buffer.append(line)
            line = buffer.pop(0)
    
            # do stuff to line
            print(line)
    
    跳过页眉行很简单我跳过页脚行时遇到问题,因为:

    • 我不想以任何方式手动更改文件
    • 我不想数文件中的行数
    • 我不想将整个文件存储在列表中(即readlines())^
    ^注意:如果您不介意将整个文件存储在内存中,可以使用以下方法:

    path = r'path\to\file.csv'
    header = 2
    footer = 2
    
    with open(path, 'r') as f:
        for line in f.readlines()[header:-footer if footer else None]:
            # do stuff to line
            print(line)
    

    如果知道文件的页眉/页脚行数,则可以使用此方法

    path = r'path\to\file.csv'
    header = 2
    footer = 2
    buffer = []
    
    with open(path, 'r') as f:
        for _ in range(header):
            f.readline()
    
        for _ in range(footer):
            buffer.append(f.readline())
    
        for line in f:
            buffer.append(line)
            line = buffer.pop(0)
    
            # do stuff to line
            print(line)
    
    跳过页眉行很简单我跳过页脚行时遇到问题,因为:

    • 我不想以任何方式手动更改文件
    • 我不想数文件中的行数
    • 我不想将整个文件存储在列表中(即readlines())^
    ^注意:如果您不介意将整个文件存储在内存中,可以使用以下方法:

    path = r'path\to\file.csv'
    header = 2
    footer = 2
    
    with open(path, 'r') as f:
        for line in f.readlines()[header:-footer if footer else None]:
            # do stuff to line
            print(line)
    

    您的
    返回或分析行的代码的其余部分在哪里?请包括整个相关代码,并使用适当的缩进。
    data=False
    行应位于末尾!不只是在前面
    如果data=True:
    您还没有包括所有相关代码。请包括从包含
    def
    的行开始的代码,以包含
    return
    的行结束。您
    返回的代码或解析该行的代码的其余部分在哪里?请包括整个相关代码,并使用适当的缩进。
    data=False
    行应位于末尾!不只是在前面
    如果data=True:
    您还没有包括所有相关代码。请包括从包含
    def
    的行开始,以包含
    return
    的行结束的代码。如果您特别想要
    列表
    ,您可以始终将
    元组
    转换为
    列表
    。如果您特别想要
    列表
    ,您始终可以将
    元组
    强制转换为
    列表