2013-10-30 73 views
-2

如果该行中有'command',我想从文本文档中提取每行的第二个数字。我希望命令和该行的其余部分打印在那些数字旁边。有数百行。如果符合条件,python会提取第二个数字

的线条看起来像:

1376328501.285|1166703600|0|SimControl|4|Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62 

此行,如果编程我需要怎么应该出来

1166703600 Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62 

我该怎么做呢?

+6

欢迎来到Stack Overflow!看起来你希望我们为你写一些代码。尽管许多用户愿意为遇险的编码人员编写代码,但他们通常只在海报已尝试自行解决问题时才提供帮助。证明这一努力的一个好方法是包含迄今为止编写的代码,示例输入(如果有的话),期望的输出和实际获得的输出(控制台输出,堆栈跟踪,编译器错误 - 无论是适用)。您提供的细节越多,您可能会收到的答案就越多。 –

+0

我是新人。我尝试了一堆事情几天,很失落,我不知道该怎么做。无论如何感谢您的帮助。 – user2938428

回答

3

像对待CSV数据(尽管由竖线)中的数据,与所述csv module

import csv 

with open('inputfile', 'rb') as inputfile: 
    reader = csv.reader(inputfile, delimiter='|') 
    for row in reader: 
     if len(row) > 5 and row[5].lower().startswith('command'): 
      print row[1], row[5] 

csv.reader()的给你一个迭代得到的每一行的列表;你的样品线会导致:

['1376328501.285', '1166703600', '0', 'SimControl', '4', 'Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62'] 

指数从0开始,所以用Command文本列是row[5];第二列号在row[1]。上面的代码测试当前行是否有足够的列,并且如果小写的话row[5]以字command开头。

上面假定Python 2;为Python 3看起来略有不同:

import csv 

with open('inputfile', newline='') as inputfile: 
    reader = csv.reader(inputfile, delimiter='|') 
    for row in reader: 
     if len(row) > 5 and row[5].lower().startswith('command'): 
      print(row[1], row[5]) 
+0

出于某种原因,我在'row [1],row [5]'行'指向'row'的'w'时出现语法错误。有任何想法吗? – user2938428

+0

@ user2938428:您是否正在使用Python 3?这个答案使用Python 2语法。 –

+0

即时通讯使用python 2.7 – user2938428

0
>>> l = """1376328501.285|1166703600|0|SimControl|4|Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62""" 
>>> l = [l,l,l] 

>>> [ele.split("|")[1] for ele in l if "command" in ele.lower()] 
['1166703600', '1166703600', '1166703600'] 
0
lines = '1376328501.285|1166703600|0|SimControl|4|Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62' 

if 'Command' in lines: 
    lines_lst = lines.split('|') 
    what_you_want = lines_lst[1] + ' '+ lines_lst[-1] 

print what_you_want 
>>> 1166703600 Command 72FB0007: AC28200 - "Thrst History Reset" to DCDR 0 time=62 

所以,如果你有一个包含成千上万行的像这样的文件:

f = open(YOUR_FILE, 'r') 
data = f.readlines() 
f.close() 

foo = [] 
for lines in data: 
    if 'Command' in lines: 
     lines_lst = lines.split('|') 
     what_you_want = lines_lst[1] + ' '+ lines_lst[-1] 
     foo.append(what_you_want) 
0
import re 

s = ''' 
1376328501.285|1166703600|0|SimControl|4|Command aaaaa 
12347801.2|11660|0|Sim|5|Command bbb 
13587918501.1|13|0|XCF|6|cccccc 
101.285|285|0|pof|7|ddddd 
137501|-2.87|457|run|8|Command eeee 
''' 
print s 

regx = re.compile('^[^|]+\|([^|]+).+?(Command.+\n?)', 
        re.MULTILINE) 

print ''.join('%s %s' % m.groups() for m in regx.finditer(s)) 

结果

1376328501.285|1166703600|0|SimControl|4|Command aaaaa 
12347801.2|11660|0|Sim|5|Command bbb 
13587918501.1|13|0|XCF|6|cccccc 
101.285|285|0|pof|7|ddddd 
137501|-2.87|457|run|8|Command eeee 

1166703600 Command aaaaa 
11660 Command bbb 
-2.87 Command eeee