从jsonarrays或json行缓慢地流式处理对象。。
json-streams的Python项目详细描述
json流
读写JSON延迟,尤其是JSON数组。在
处理两种JSON格式:
[{"a":1},{"a":2}]
以及JSON行格式:
^{pr2}$使用orjson
或{json
。在
使用
安装
# Using standard json pip install json-streams # Using orjson pip install json-streams[orjson]# Using ujson pip install json-streams[ujson]
注
此库首选以二进制模式打开的文件。
因此,所有dumps
-方法都返回bytes
。在
所有loads
方法处理str
参数。
如果使用orjson
库,还可以将bytes
或{loads
。
目标是使所有loads
处理str
、bytes
和{
示例
允许您使用json.load
和{
importjson_streams# This command tries to guess format and opens the filedata=json_streams.load_from_file("data.json")# or data.jsonl# Write to file, again guessing formatjson_streams.dump_to_file(data,"data.jsonl")
fromjson_streamsimportjson_iter,jsonl_iter# Open and read the file without guessingdata=json_iter.load_from_file("data.json")# Process file# Write to file without guessingjsonl_iter.dump_to_file(data,"data.jsonl")
importjson_streamsdefprocess(data):forentryindata:# processyieldentrydefread_process_and_write(filename_in,filename_out):json_streams.dump_to_file(process(json_streams.load_from_file(filename_in)),filename_out)
发展
复制回购后,只需运行
$ make test
要设置虚拟环境, 安装开发依赖项 然后运行单元测试。在
注意:如果在激活的虚拟环境中运行该命令, 而是使用这种环境。在
- 项目
标签: