我有个问题,我现在正忙着呢。
我有一个大文件,格式如下:
区块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等
我想阅读每个块的第一行,以检查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行,将它们放在同一行中。继续这样做,直到找到Type 2,并对第3行和第8行执行相同的操作,并将其存储在不同的输出文件中。
我希望我没有把事情复杂化。
编辑:
对不起,我说得不清楚,也犯了个错误。
在第1行:/我不感兴趣之前的第一部分。我对此很感兴趣,因为它有时会给Type1或Type2带来机会。
理想情况下,输出应该是,在第一行中查找Type,如果Type2输出:
Line 1: Type2 Line 3: Data we need Line 8: Data we need如果Type1:
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会按类型进行排序。
谢谢
发布于 2013-09-17 21:39:23
看看下面的代码是否给了您解决问题所需的灵感--我敢打赌是这样的:
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() https://stackoverflow.com/questions/18859705
复制相似问题