[英]Parsing log file and writing it into csv file efficiently
我有一個使用正則表達式解析的日志文件。 它給了我3個元素
1)時間戳
2)numberid
3)對象值
我打算將此文件有效地寫入CSV文件中(因為日志文件的大小可能很大)。
我已經試過了
def read_logs(input_file):
data = defaultdict()
for each in input_file:
regex_match = re(r'',each)
data['timestamp'].append(regex_match.group(1))
data['numberid'].append(regex_match.group(2))
data['objectvalue'].append(regex_match.group(3))
return data
def main(inputname,outputname):
with open(inputname) as input_file:
data = read_logs(input_file)
with open(outputname,'w') as out_file:
write_file(out_file,data)
def write_file(out_file):
out = csv.writer(out_file)
out.writerow(['timestamp_val','numberid','objectvalue'])
1)我認為使用defaultdict將是將此類數據寫入文件的最有效方法。 這里的defaultdict鍵是timestamp
numberid
和以list為值的obejctvalue
。 如何在CSV文件中編寫此代碼?
樣本數據值為
data = ('timestamp_val':['10:10:54','13:02:07','03:02:10'],'numberid':[AA10,BB18,FF34],'objectvalue':['NHAG','ABCD','YTAB'])
2)如果這不是一種有效的方法,那么有什么更好的方法可以做到這一點?
換句話說,我想到的是使用正則表達式讀取每一行並同時在CSV文件中寫入。 這是一個好方法嗎?
我認為您不需要讀取列表dic中的所有文件:讀取后立即寫入
def main(inputname,outputname):
with open(inputname) as input_file, open(outputname,'w') as out_file:
out = csv.writer(out_file)
out.writerow(['timestamp_val','numberid','objectvalue'])
for each in input_file:
regex_match = re(r'',each)
out.writerow([regex_match.group(1), regex_match.group(2), regex_match.group(3)])
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.