2017-02-24 16 views
1

我試圖從文件中提取一些信息。該文件有很多行像下面在一行中查找圖案並在括號中打印以下值

"names":["DNSCR"],"actual_names":["RADIO_R"],"castime":[2,4,6,8,10] ......  

我想在每一行名稱和castime進行搜索,如果發現我想在括號 在括號內的值在不同的線路改變打印值的一個。例如在上面的行中是DNSCR,casttime是2,3,6,8。但長度可能 是不同的在下一行

我已經嘗試了下面的代碼,但它會一直給我10個字符,但我只需要在括號中只。

c_req = 10 
keyword = ['"names":','"castime":'] 

with open('mylogfile.log') as searchfile: 
    for line in searchfile: 
     for key in keywords: 
      left,sep,right = line.partition(key) 

      if sep: 

        print key + " = " + (right[:c_req]) 
+0

該文件是否包含不一致的引號(如您的示例中)? –

+0

該文件實際上具有一致的報價。錯誤的副本 –

回答

0

這看起來就像json一樣,每條線周圍是否有括號? 如果是這樣,整個內容是微不足道解析:

import json 
test = '{"names":["DNSCR"],"actual_names":["RADIO_R"],"castime":[2,4,6,8,10]}' 
result = json.loads(test) 
print(result["names"], result["castime"]) 

您也可以使用像熊貓庫整個文件讀入一個數據幀,如果它匹配的整體JSON文件。

+0

否每個行周圍都沒有括號。這些線條實際上很長,沒有特定的圖案,我在這裏展示的是我很有趣的。 –

+0

如果你能展示整個結構,這將有所幫助。 –

0

使用正則表達式:

import re 

# should contain all lines 
lines = ['"names":["DNSCR"],"actual_names":["RADIO_R"],"castime":[2,4,6,8,10]'] 

# more efficient in large files 
names_pattern = re.compile('"names":\["(\w+)"\]') 
castime_pattern = re.compile('"castime":\[(.+)\],?') 

names, castimes = list(), list() 

for line in lines: 
    names.append(re.search(names_pattern, line).group(1)) 
    castimes.append(
     [int(num) for num in re.search(castime_pattern, line).group(1).split(',')] 
    ) 

添加異常處理和文件打開/讀取

0

鑑於mylogfile.log

"names":["DNSCR"],"actual_names":["RADIO_R"],"castime":[2,4,6,8,10] 
"names":["FOO", "BAR"],"actual_names":["RADIO_R"],"castime":[1, 2, 3] 

使用正則表達式和ast.literal_eval

import ast 
import re 

keywords = ['"names":', '"castime":'] 
keywords_name = ['names', 'castime'] 

d = {} 

with open('mylogfile.log') as searchfile: 
    for i, line in enumerate(searchfile): 
     d['line ' + str(i)] = {} 
     for key, key_name in zip(keywords, keywords_name): 
      d['line ' + str(i)][key_name] = ast.literal_eval(re.search(key + '\[(.*?)\]', line).group(1)) 
print(d) 

#{ 'line 0': {'castime': (2, 4, 6, 8, 10), 'names': 'DNSCR'}, 
# 'line 1': {'castime': (1, 2, 3), 'names': ('FOO', 'BAR')}} 

re.search(key + '\[(.*?)\]', line).group(1)將抓住一切是在[]之間的keys後。

而且ast.literal_eval()將改變去除usless報價和空間在您的string,並在需要時自動創建tuples。我也使用enumerate來跟蹤它在日誌文件中的哪些行。