0

我遇到了一个我目前遇到的问题。

我有一个以下格式的大文件:

区块 1

Line 1: Something/Type2
Line 2: Time
Line 3: Data we need
Line 4: 00.*
Line 5: Fix 100
Line 6: In..
Line 7: Ou..
Line 8: Data we need
Line 9: Next
Line 10: Multi_Exit

第 2 座

Line 1: Something/Type1
Line 2: Time
Line 3: Data we need
Line 4: 00.*
Line 5: Fix 100
Line 6: In..
Line 7: Ou..
Line 8: Data we need
Line 9: Next
Line 10: Multi_Exit

区块 3

Line 1: Something/Type1
Line 2: Time
Line 3: Data we need
Line 4: 00.*
Line 5: Fix 100
Line 6: In..
Line 7: Ou..
Line 8: Data we need
Line 9: Next
Line 10: Multi_Exit

第 4 座

Line 1: Type1/Type2
Line 2: Time
Line 3: Data we need
Line 4: 00.*
Line 5: Fix 100
Line 6: In..
Line 7: Ou..
Line 8: Data we need
Line 9: Next
Line 10: Multi_Exit

ETC

我想读取每个块的第一行,以检查是 Type1 还是 Type2。在此之后,我想打印每个块的第 3 行和第 8 行并继续这样做直到文件结束。

我尝试了以下代码:

p = './file.txt'
fin = open(p, 'r')

for i, line in enumerate(fin):
if i%11 == 2 or i%11 == 7:
    print line
fin.close()

在我的大文件上运行此代码后,我注意到行发生了变化。我只能假设我的块长度不固定为 10 行(在下一个块开始之前加上一个行空间)。所以这种方法并不理想。

我也尝试过正则表达式,但我无法以我想要的格式存储结果,例如:

For Type 1

文件输出应为: 第 3 行:数据 第 8 行:数据

它之间的单个空间。

这是我尝试过的下一个代码:

for line in fin:
if re.match("(Line 1|Line 3|Line 8)", line):
    writeToFile(line)

其中 writeToFile 函数执行以下操作:

def writeToFile(filein):
    p = './output.txt'
    fo = open(p, 'a')
    fo.write(filein)
    fo.close()

这是 output.txt 文件的外观:

Line 1: Something/Type2

Line 3: Data we need

Line 8: Data we need


Line 1: Something/Type1

Line 3: Data we need

Line 8: Data we need


Line 1: Something/Type1

Line 3: Data we need

Line 8: Data we need

这并不完全是预期的结果。我什至不介意玩弄这个输出文件并检查第 1 行是否是类型 1。然后将第 3 行和第 8 行放在同一行中。继续这样做,直到找到类型 2 并对第 3 行和第 8 行执行相同操作并将其存储在不同的输出文件中。

我希望我没有把事情复杂化。

编辑:

对不起,我不清楚,也犯了一个错误。

在第 1 行:/ 之前的第一部分我不感兴趣。之后我对它很感兴趣,有时可能会选择 Type1 或 Type2。

理想情况下,输出应该是,在第一行查找 Type,如果 Type2 输出:

Line 1: Type2 Line 3: Data we need Line 8: Data we need

如果类型 1:

Line 1: Type1 Line 3: Data we need Line 8: Data we need
Line 1: Type1 Line 3: Data we need Line 8: Data we need

对所有具有相同类型的块进行分组。

编辑:感谢用户:Floris,我现在得到了我想要的输出

如果我将它提供给我的写入文件功能。

def writeToFile(type, outputString):
    p = './output'+type+'.txt'
    fo = open(p, 'a')
    line = '%s %s\n' % (type, outputString)
    fo.write(line)
    fo.close()

这得到了我的结果:

Type2 Line 3: Data we need Line 8: Data we need

Type1 Line 3: Data we need Line 8: Data we need
Type3 Line 3: Data we need Line 8: Data we need

当我指定如何将其保存为类型路径时,我的 writeToFile 按类型对其进行排序。

谢谢

4

1 回答 1

0

看看下面的代码是否能给你解决问题所需的灵感——我敢打赌:

import re
fin = open("./file.txt")

for line in fin:
    if re.match("Line 1:", line):
      # note we need to match "Line 1:" (including colon) so we don't match "Line 10"
      m = re.match(".*Type(.)", line)
      type = m.group(1)
      # we now know what type this group is
    if re.match("Line 3:", line):
      m = re.match(".*3:(.*)$", line)
      outputString = m.group(1)
      # have first half of output string
    if re.match("Line 8:", line):
      m = re.match(".*8:(.*)$", line)
      outputString += m.group(1)
      # have second half of output string, and we know where it needs to go:
      print "concatenated string of type ", type," is ", outputString
      # now send it where you want it to go... one of two open files, perhaps?

fin.close()    
于 2013-09-17T21:39:23.677 回答