python - 解析换行分隔文件

标签 python fileparsing

我正在做一个项目,我想在其中使用 Python 解析文本文件。该文件由一些格式不同的 block 的数据条目组成。有新行时会找到新条目。这是我想要完成的:

  1. 跳过前几行(前 16 行)
  2. 在第16行之后,有一个换行符开始新的数据输入
  3. 阅读以下行,直到遇到新的换行符。每行都附加到一个名为数据的列表中。
  4. 列表将被传递给处理进一步处理的函数。
  5. 重复步骤 3 和 4,直到文件中没有更多数据

这是文件的一个例子:

Header Info
More Header Info

Line1
Line2
Line3
Line4
Line5
Line6
Line7
Line8
Line9
Line10
Line11
Line12
Line13

MoreInfo    MoreInfo    MoreInfo    MoreInfo    MoreInfo
MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2
MoreInfo3   MoreInfo3   MoreInfo3   MoreInfo3   MoreInfo3
MoreInfo4   MoreInfo4
FieldName1  0001    0001
FieldName1  0002    0002
FieldName1  0003    0003
FieldName1  0004    0004
FieldName1  0005    0005
FieldName2  0001    0001
FieldName3  0001    0001
FieldName4  0001    0001
FieldName5  0001    0001
FieldName6  0001    0001

MoreInfo    MoreInfo    MoreInfo    MoreInfo    MoreInfo
MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2   MoreInfo2
MoreInfo3   MoreInfo3   MoreInfo3   MoreInfo3   MoreInfo3
MoreInfo4   MoreInfo4
FieldName1  0001    0001
FieldName1  0002    0002
FieldName1  0003    0003
FieldName1  0004    0004
FieldName1  0005    0005
FieldName2  0001    0001
FieldName3  0001    0001
FieldName4  0001    0001
FieldName5  0001    0001
FieldName6  0001    0001

这是我编写的一些代码。它能够读取第一个 block 并将其附加到列表中:

with open(loc, 'r') as f:
    for i in range(16):
        f.readline()

    data = []
    line = f.readline()
    if line == "\n":
        dataLine = f.readline()
        while dataLine != "\n":
            data.append(dataLine)
            dataLine = f.readline()

    #pass data list to function
    function_call(data)
    # reset data list here?
    data = []

我怎样才能使它适用于整个文件?我的假设是使用“with open”,它充当“while not end of file”。在跳过前 16 行后,我尝试添加一个“while True”。 我对 Python 的解析能力知之甚少。

在此先感谢您的帮助。

最佳答案

在初始跳过之后添加一个 while True 应该绝对有效。当然,您必须正确了解所有细节。

可以尝试扩展您已有的方法,在外循环中使用嵌套的while 循环。但将其视为单个循环可能更容易。对于每一行,您可能只需要做三件事:

  • 如果 没有行,因为你在 EOF,break 退出循环,确保处理旧的数据 (文件中的最后一个 block )如果有第一个。
  • 如果是空行,则开始一个新的data,确保先处理旧的data(如果有的话)。
  • 否则,附加到现有的数据

所以:

with open(loc, 'r') as f:
    for i in range(16):
        f.readline()

    data = []
    while True:
        line = f.readline()
        if not line:
            if data:
                function_call(data)
            break
        if line == "\n":
            if data:
                function_call(data)
                data = []
        else:
            data.append(line)

有几种方法可以进一步简化:

  • 使用 for line in f: 而不是重复执行 f.readline() 并检查它的 while 循环。<
  • 使用groupby将行迭代器转换为以空行分隔的行组迭代器。

关于python - 解析换行分隔文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30447850/

相关文章:

python - MacPorts- 端口 : command not found?

python - django2.1 发送电子邮件失败 :ssl. SSLError : [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl. c:833)

c - 在文件中搜索行时使用哪种方法

python - 尝试打开现有文件时出现 IOError

c# - C#中的文件解析

linux - grep -A <num> 直到一个字符串

javascript - 使用 D3.js 解析上传的 CSV 文件

c++ - 重用套接字时出现套接字使用错误

python - 使用 psycopg2 将数据从 Postgresql 提取到 python 的最快/最有效的方法是什么

python - Matplotlib ArtistAnimation 给出 TypeError : 'AxesImage' object is not iterable