我有一个以下格式的文件;我想用pyhton和c ++解析它并在ImpVarNo之后提取数字: 这种格式有很多行。
sample.txt的
Start:
abc pqr
(FF_GGGGG_CONFIRM_TR):TC:20222,SeqNum:86,ImpVarNo:1000000008234436,Id:12,oneId:66454,a/c:1,ImpValue:905,Impvar:25,actualValue:905,actualVar:25,abc pqr xyz
Impquantity:0,pgb ncr yepp
Start:
abc pqr
(FF_GGGGG_CONFIRM_TR):TC:20222,SeqNum:86,ImpVarNo:1000000008234436,Id:12,oneId:66454,a/c:1,ImpValue:905,Impvar:25,actualValue:905,actualVar:25,abc pqr xyz
Impquantity:0,pgb ncr yepp
Start:
abc pqr
(FF_GGGGG_CONFIRM_TR):TC:20222,SeqNum:86,ImpVarNo:1000000008234436,Id:12,oneId:66454,a/c:1,ImpValue:905,Impvar:25,actualValue:905,actualVar:25,abc pqr xyz
Impquantity:0,pgb ncr yepp
Start:
abc pqr
(FF_GGGGG_CONFIRM_TR):TC:20222,SeqNum:86,ImpVarNo:1000000008234436,Id:12,oneId:66454,a/c:1,ImpValue:905,Impvar:25,actualValue:905,actualVar:25,abc pqr xyz
Impquantity:0,pgb ncr yepp
所以我写了下面的代码:
#!/usr/bin/env python
import sys
import re
hand = open('newlogfile.txt')
for line in hand:
r = re.compile("ExOrderNo:(\d+),")
print r
答案 0 :(得分:0)
import re
with open('newlogfile.txt') as f:
r = re.compile("ImpVarNo:(\d+),")
for line in f:
inp = r.findall(line)
if inp:
print(float(inp[0]))
1000000008234436
1000000008234436
1000000008234436
1000000008234436
如果该行总是以相同的东西开头:
import re
with open('newlogfile.txt') as f:
r = re.compile("ImpVarNo:(\d+),")
for line in f:
if line.startswith("(FF_GGGGG_CONFIRM_TR)"):
print(r.findall(line))
答案 1 :(得分:-2)
然而,这是最简单的方法,您可以使用RegEx
来使代码更清晰。
sample_file = open(sample.txt)
contents = sample_file.readlines()
for line in contents:
if line.startswith("(FF_GGGGG_CONFIRM_TR)"):
number_after_impvarno = int(line.split(",")[2][9:])