2014-09-23 36 views
-2

我有一个文件(test.txt的)和内容如下:找到某些字在文件中的python

I0914 17:37:15.763941 29832 abc.cpp:138] Iteration 0, Testing (#0) 
I0922 16:14:14.933842 2057 abc.cpp:176] Test score #0: 0.146329 
I0914 17:37:15.763941 29832 abc.cpp:138] Iteration 1000, Testing (#0) 
I0922 16:14:14.933842 2057 abc.cpp:176] Test score #0: 0.246222 
I0914 17:37:15.763941 29832 abc.cpp:138] Iteration 2000, Testing (#0) 
I0922 16:14:14.933842 2057 abc.cpp:176] Test score #0: 0.335429 
I0914 17:37:15.763941 29832 abc.cpp:138] Iteration 3000, Testing (#0) 
I0922 16:14:14.933842 2057 abc.cpp:176] Test score #0: 0.445429 
I0914 17:37:15.763941 29832 abc.cpp:138] Iteration 4000, Testing (#0) 
I0922 16:14:14.933842 2057 abc.cpp:176] Test score #0: 0.546429 

,我的问题是,如何让迭代(0,1000,2000年的数量.. 。,4000)和考试分数(0.146329,0.246222,0.335429 .... 0.546429)并将它们组合成字典。

例如,我的预期结果如下:提前

dict = {'0':0.146329, 
     '1000':0.246222 
     '2000':0.335429 
     '3000':0.445429 
     '4000':0.546429} 

感谢。

+0

@Fledgling我已经读文件到“行”,并找到关键字(迭代和分数),像得分= [线线线线,如果“分数”],ITER = [如果行中有“迭代”行,则将行分隔),然后我不知道要解决问题... – RyanLiu 2014-09-23 03:01:04

回答

1
iter = 0 
for line in file: 
    itermatch = re.search('Iteration \d+',line) 
    if itermatch: 
    iter = itermatch.group() 
    else: 
    scorematch = re.search(': [0-9.]+',line) 
    if scorematch: 
     dict[iter]= scorematch.group() 
0

这是为了做到这一点,而无需使用正则表达式的一种方式:

result = {} 
with open('test.txt') as in_file: 
    for line in in_file: 
     data = line.strip().split('] ')[1] 
     if ',' in data: 
      key = data.split(',')[0] 
      key = key.split(' ')[1] 
     else: 
      val = (data.split(':')[1]).strip() 
      print val 
      result[key] = val 

这给:

{'0': '0.146329', 
'1000': '0.246222', 
'2000': '0.335429', 
'3000': '0.445429', 
'4000': '0.546429'} 
相关问题