如何使用 Python 对每行数千个词典的文件进行数据分析
How to do data analysis using Python, of a file with thousands of dictionaries in each line
我目前有一个文件有 5000 行,每行一个字典。所有词典都有相同的字段。我的问题是:
我应该学习 SQL 来存储这些数据并用它进行分析,还是使用我已经足够好的文件,我应该只使用 pandas 或其他一些模块做数据分析。
我真的不知道该走哪条路
虽然这个问题很笼统 - 应该注意 我如何存储我的数据集 和 我使用什么工具来分析我的数据集的问题data 是非常不同的问题。
通常对于需要定期修改或更新的数据集,数据库比压缩文件更可取(因为修改压缩文件内容将需要您重写所有数据)。例如,我可能不会将 sqlite
用于 nltk.corpus
,尽管也可能有用例。
如果您决定使用 with sqlite
并且您的原始数据是字典格式,尤其是对于许多字段 -
您可能会发现 exectrace
和 rowtrace
很有用:
http://apidoc.apsw.googlecode.com/hg/connection.html#apsw.Connection.setrowtrace
和
http://apidoc.apsw.googlecode.com/hg/connection.html#apsw.Connection.setexectrace
有用。
例如,要从 sqlite
中获取 dict
中的行而不是元组格式,您可以这样做:
def rowtracer(cursor, sql):
dictionary = {}
for index, (name, type_) in enumerate(cursor.getdescription()):
dictionary[name] = sql[index]
return dictionary
con.setrowtrace(rowtracer)
对于插入,您可以通过
在字典中传递值
"""insert into my_table(name, data) values(:name, :date)"""
我目前有一个文件有 5000 行,每行一个字典。所有词典都有相同的字段。我的问题是:
我应该学习 SQL 来存储这些数据并用它进行分析,还是使用我已经足够好的文件,我应该只使用 pandas 或其他一些模块做数据分析。
我真的不知道该走哪条路
虽然这个问题很笼统 - 应该注意 我如何存储我的数据集 和 我使用什么工具来分析我的数据集的问题data 是非常不同的问题。
通常对于需要定期修改或更新的数据集,数据库比压缩文件更可取(因为修改压缩文件内容将需要您重写所有数据)。例如,我可能不会将 sqlite
用于 nltk.corpus
,尽管也可能有用例。
如果您决定使用 with sqlite
并且您的原始数据是字典格式,尤其是对于许多字段 -
您可能会发现 exectrace
和 rowtrace
很有用:
http://apidoc.apsw.googlecode.com/hg/connection.html#apsw.Connection.setrowtrace
和
http://apidoc.apsw.googlecode.com/hg/connection.html#apsw.Connection.setexectrace
有用。
例如,要从 sqlite
中获取 dict
中的行而不是元组格式,您可以这样做:
def rowtracer(cursor, sql):
dictionary = {}
for index, (name, type_) in enumerate(cursor.getdescription()):
dictionary[name] = sql[index]
return dictionary
con.setrowtrace(rowtracer)
对于插入,您可以通过
在字典中传递值"""insert into my_table(name, data) values(:name, :date)"""