在Python中解析CSV日志文件

2024-06-16 12:54:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个web日志数据,我需要解析并加载到Pandas dataframe中,以运行一些机器学习算法。问题是输入文件,虽然csv不是直接的逗号分隔格式。它包括url参数和json格式的键值对,我还需要对它们进行解析,所有这些都是用unicode编码的。在

输入CSV如下所示:

[Row(firstKey=u'some_alpha_nemeric', anotherKey=u'another_alpha_nemeric_value',referrer=u'//google.com/images/etc.html?fid=283000185&flowid=17001,something_in_json_format={u'availibility': u'available', u'lifecycle': u'-'},
[Row(....

输出pandas dataframe应该读取所有键的值,包括URL参数(如上面示例中的referer URL)和jSON值

所以基本上我需要一个输出pandas dataframe,列为firstKey, anotherKey, refferrer, fid, flowid, something_in_json_format.availibility, something_in_json_format.lifecycle

我已经开始编写一个基本的csv阅读器脚本

^{pr2}$

但我不知道如何解析这个复杂的结构并加载到pandas中。在


Tags: csvinalphajsonformatdataframepandas参数