我有一些代碼來解析Apache日誌文件(start_search
和end_search
是在Apache日誌中找到的格式的日期字符串):爲什麼我不能分割這個python列表?
with open("/var/log/apache2/access.log",'r') as log:
from itertools import takewhile, dropwhile
s_log = dropwhile(lambda L: start_search not in L, log)
e_log = takewhile(lambda L: end_search not in L, s_log)
query = [line for line in e_log if re.search(r'GET /(.+veggies|.+fruits)',line)]
import csv
query_dict = csv.DictReader(query,fieldnames=('ip','na-1','na-2','time', 'zone', 'url', 'refer', 'client'),quotechar='"',delimiter=" ")
import re
veggies = [ x for x in query_dict if re.search('veggies',x['url']) ]
fruits = [ x for x in query_dict if re.search('fruits',x['url']) ]
第二列表生成總是空的;也就是說,如果我切換最後兩行的順序:
fruits = [ x for x in query_dict if re.search('fruits',x['url']) ]
veggies = [ x for x in query_dict if re.search('veggies',x['url']) ]
第二個列表總是空的。
爲什麼? (以及如何填充fruits
和veggies
列表?)
調用它'query_dict'不會使它成爲'dict'。 – kindall
@kindall也許更有建設性的評論會是''csv.DictReader'不返回列表字典,而是一個迭代器「。 – Jamie