python中json行文件的快速更新方法

2024-05-16 14:41:37 发布

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

我有一个jsonline文件如下:

{"id":0,"country":"fr"}
{"id":1,"country":"en"}
{"id":2,"country":"fr"}
{"id":3,"country":"fr"}

我有一个代码列表,我想通过更新文件行为每个用户指定一个代码。你知道吗

结果如下:

{"id":0,"country":"fr", code:1}
{"id":1,"country":"en", code:2}
{"id":2,"country":"fr", code:3}
{"id":3,"country":"fr", code:4}

我现在就是这样做的:

import ujson
fh, abs_path = mkstemp()

with open(fh, 'w') as tmp_file:
    with open(shooting.segment_filename) as segment_filename:
        for line in segment_filename:
            enriched_line = ujson.loads(line)
            code = compute_code()
                if code:
                    enriched_line["code"] = code
            tmp_file.write(ujson.dumps(enriched_line) + '\n')

我的问题是,有没有更快的方法?例如,可能是通过sarge启动的linux命令?或者任何不必读/写/替换原始文件的pythonic方法?你知道吗

谢谢你!你知道吗


Tags: 文件代码idwithlinesegmentcodeopen
2条回答

我不知道这是否能让你满意,但这里有一些“更干净”的代码:

import json

with open(shooting.segment_filename, "r") as f:
    data = [json.loads(line) for line in f.readlines()]

for json_line in data:
    code = compute_code()
    if code:
        json_line["code"] = code

# Will overwrite source file, you might want to give a bogus file to test it first
with open(shooting.segment_filename, "w") as f:
    f.write("\n".join([json.dumps(elem) for elem in data]))

为了提高性能,您可以完全跳过json序列化/反序列化步骤,只需将右括号替换为代码+右括号。你知道吗

所以这应该表现得更好:

content = ""
with open("inp.txt", "r") as inp:
    for line in inp:
        content += line[:-1] + ", code:%s}\n" % compute_code()

with open("inp.txt", "w") as out:
    out.write(content)

编辑: 如果不想将整个文件加载到内存中,可以这样做。你知道吗

with open("inp.txt", "r") as inp, open("out.txt", "w") as out:
    for line in inp:
        out.write(line[:-1] + ", code:%s}\n" % compute_code())

相关问题 更多 >