【发布时间】:2021-03-29 06:45:03
【问题描述】:
我已经下载了 zip 文件并从中提取了日志文件,但是,我无法打开 .log 文件格式并将数据合并到数据框中。
import fnmatch
with ZipFile("path/HTWebLog_p1.zip") as zipfiles:
file_list = zipfiles.namelist()
#get only the .log files
csv_files = fnmatch.filter(file_list, "*.log")
#iterate with a list comprehension to get the individual dataframes
data = [pd.read_csv(zipfiles.open(file_name), delimiter=',', header=0) for file_name in csv_files]
#combine into one dataframe
df = pd.concat(data)
df.head()
csv_files 数据框的输出以 .log 格式显示 zip 文件的名称。 我收到以下错误。 ParserError:错误标记数据。 C 错误:第 5 行中应有 1 个字段,但看到了 3
数据:
#Software: Microsoft Internet Information Services 6.0
#Version: 1.0
#Date: 2006-11-01 00:00:08
#Fields: date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Referer) sc-status sc-substatus sc-win32-status
2006-11-01 00:00:08 W3SVC1 127.0.0.1 GET /Default.aspx - 80 - 70.80.84.76 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) http://www.google.com/search?sourceid=navclient&aq=t&ie=UTF-8&rls=GGLD,GGLD:2005-19,GGLD:en&q=Tulip+hotel 200 0 0
2006-11-01 00:00:08 W3SVC1 127.0.0.1 GET /Tulip/home/en-us/home_index.aspx - 80 - 70.80.84.76 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) - 200 0 0
2006-11-01 00:00:08 W3SVC1 127.0.0.1 GET /Tulip/includes/js/CommonUtil.js - 80 - 70.80.84.76 Mozilla/4.0+(compatible;+MSIE+6.0;+Windows+NT+5.1;+SV1) http://www.hotelTulip.com.hk/Tulip/home/en-us/home_index.aspx 200 0 0
【问题讨论】:
标签: python pandas csv zipfile logfile