我的文本文件的行如下。這種類型的行在文件中多次出現。需要從Python的括號內的文本文件中讀取數據
[年11月22 22點27分13秒] INFO - [com.macys.seo.business.impl.LinkBusinessImpl] - 執行搜索(WS)網關請求:KeywordVO(關鍵字= GUESS得分= 83965 normalizedKeyword = GUESS productIds = [] categoryIds = [] hotListed =假列入黑名單=假globalHotList =假URL = /購買/猜測)
我想僅在以下數據中提取到一個文件,如:
關鍵字=猜測,分數= 83965,hotListed = false,globalHotList = false url =/buy/GUESS
這是我到目前爲止有:
def get_sentences(filename):
with open('d:\log.log') as file_contents:
d1, d2 = '(', ')' # just example delimiters
for line in file_contents:
if d1 in line:
results = []
elif d2 in line:
yield results
else: results.append(line)
print results
請指教。
解決方案是否必須是Python的?你堅持哪部分? – Johnsyweb
我只需要使用python。我的代碼段是def get_sentences(filename): with open('d:\ log.log')as file_contents: d1,d2 ='(',')'#只是示例分隔符 for file in file_contents: if D1在行: 結果= [] ELIF D2在行: 收效 其他: results.append(線) 打印效果 – newcane