- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到了几个讨论 ijson 在 python 中加载巨大 JSON 文件的线程,因为这是不消耗所有内存的方法。
我的文件大小约为 1.4 GB,它有多个节点(见下图),我只对一个包含大部分数据的节点 (c_driver_location) 感兴趣。
我的目标是:我只想提取 c_driver_location 节点数据并将其插入到 mysql 数据库表(它将有四列:id、longitude、latitude、timestamp)。
表ddl:
create table drv_locations_backup7May2017 (id bigint unsigned auto_increment primary key, drv_fb_id varchar(50), latitude DECIMAL(10, 8) NOT NULL, longitude DECIMAL(11, 8) NOT NULL, timestamp int )
我的问题是:我运行了附加代码的第一部分(直到连接到 mysql 之前),但它已经运行了 20 个小时,但仍然没有完成对 json 的解析。 (我在较小的文件上进行了测试,效果很好)。
是否有最佳方法可以使其更快、更高效?
import numpy as np
import pandas as pd
from pandas import Series, DataFrame
import ijson
import pymysql.cursors
import pymysql
filename = "D:\json_file.json"
drv_col_list = ['drv_fb_id','latitude','longitude','timestamp']
drv_df = DataFrame(columns = drv_col_list)
drv_df.timestamp = drv_df.timestamp.astype(int)
counter = 0
with open(filename, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix == 'c_driver_location' and str(event) == 'map_key':
drv_fb_id = value
counter = counter + 1
elif prefix.endswith('.latitude'):
latitude = value
elif prefix.endswith('.longitude'):
longitude = value
elif prefix.endswith('.timestamp'):
timestamp = value
elif prefix.endswith(drv_fb_id) and str(event) == 'end_map':
drv_df = drv_df.append(pd.DataFrame({'drv_fb_id':drv_fb_id,'latitude':latitude,'longitude':longitude,'timestamp':timestamp},index=[0]),ignore_index=True)
connection = pymysql.connect(host='53.000.00.00',
port = 3306,
user='user',
password='abcdefg',
db ='newdb',
# charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
# write to mysql
drv_df.to_sql(con=connection, name='drv_locations_backup7May2017', if_exists='replace', flavor='mysql')
connection.close()
最佳答案
您只需稍微修改代码即可生成数据转储。
import ijson
outfile = "D:\upload_data.txt"
filename = "D:\json_file.json"
drv_col_list = ['drv_fb_id','latitude','longitude','timestamp']
timestamp = drv_df.timestamp.astype(int)
ofile = open(outfile, "rw")
counter = drv_fb_id = latitude = longitude = 0
with open(filename, 'r') as fd:
parser = ijson.parse(fd)
for prefix, event, value in parser:
if prefix == 'c_driver_location' and str(event) == 'map_key':
drv_fb_id = value
counter = counter + 1
elif prefix.endswith('.latitude'):
latitude = value
elif prefix.endswith('.longitude'):
longitude = value
elif prefix.endswith('.timestamp'):
timestamp = value
elif prefix.endswith(drv_fb_id) and str(event) == 'end_map':
print >>ofile, ",".join(map(str, [drv_fb_id, latitude, longitude, timestamp]))
close(ofile)
现在您在 D:\upload_data.txt 中有一个逗号分隔的输出
代码未经测试。
我目前没有测试 mysql 数据库。我相信 mysql manual is easy to follow .你的表结构并不复杂。
关于python - 使用 ijson python 将 1.4 GB json 数据加载到 mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43910729/
我需要一点帮助来解析一个大的 JSON 文件。这里我只有一个数据样本(只有 2 个项目)。 我需要使用解析方法。 open() 不起作用,因为文件太大。 parser=ijson.parse("sam
我想从一个非常大的 json 文件中提取 abstractText。我使用 python 中的 lib ijson,但找不到正确的方法来提取我想要的内容。 json文件的结构是 {'articles'
for prefix, event, value in parser: print(prefix) 执行上述代码后出现以下错误,我不明白错误是什么。 ijson.common.Incomple
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 7 年前。 Improve t
我一直在尝试使用 Python 的 ijson 库解析 JSON 文件。这在查看二级元素或使用解析器时有效,但我更喜欢直接查看顶级元素的便利性。 JSON 的基本格式如下: {"foo":{"a":1
我有一个很大的 JSON 文件,如下所示: {"details":{ "1000":[ ["10","Thursday","1","19.89"], ["12"
传入的数据类似于以下内容: [{ "foo": "bar" }] [{ "bar": "baz" }] [{ "baz": "foo" }] 如您所见,对象数组串在一起。 JS
本教程https://www.dataquest.io/blog/python-json-tutorial/他们有一个 600MB 的文件,但是当我运行他们的代码时 import ijson file
我正在尝试解析一个大的 json 文件(数百个演出)以从其 key 中提取信息。为简单起见,请考虑以下示例: import random, string # To create a random ke
我正在处理一个非常大的 JSON 文件,我决定使用 ijson 库。 现在我想更新这个 JSON 文件中的一些值,但我不知道该怎么做。 例如,来自: {"age": 25, "gender": "M"
我正在尝试使用 ijson 而不是 json 以便能够有效地将字典转储到/从字符串(在内存中,而不是从文件中)转储/加载字典[1]. ijson 是否有任何类似于使用 json 进行标准转储/加载的示
我已经安装了python 3.7.0,我的安装目录是AppData\Local\Programs\Python\Python37-32 现在我尝试使用以下方法升级 pip:python -m pip
我正在尝试解析来自 GoogleMaps API 的 JSON 响应,以将嵌套字典中的值读取到 Pandas DataFrame 中。 JSON 响应的结构如下所示。 我正在尝试创建一个名为“addr
我正在尝试使用 ijson 包解析一个大型 (~100MB) json 文件,这使我能够以高效的方式与文件进行交互。然而,在编写了一些这样的代码之后, with open(filename, 'r')
我需要解析一个可能很大的 json 文件。所以我寻找解决方案来帮助我并找到了 ijson 库。似乎非常适合这个目的。 我使用 pypm(我使用 Active Python)、端口和直接使用提供的安装脚
我遇到了几个讨论 ijson 在 python 中加载巨大 JSON 文件的线程,因为这是不消耗所有内存的方法。 我的文件大小约为 1.4 GB,它有多个节点(见下图),我只对一个包含大部分数据的节点
我是一名优秀的程序员,十分优秀!