Python: make a list generator JSON serializable

后端 未结 4 1576
旧巷少年郎
旧巷少年郎 2020-12-29 05:23

How can I concat a list of JSON files into a huge JSON array? I\'ve 5000 files and 550 000 list items.

My fist try was to use jq, but it looks like jq -s is not opt

4条回答
  •  心在旅途
    2020-12-29 05:56

    You should derive from list and override __iter__ method.

    import json
    
    def gen():
        yield 20
        yield 30
        yield 40
    
    class StreamArray(list):
        def __iter__(self):
            return gen()
    
        # according to the comment below
        def __len__(self):
            return 1
    
    a = [1,2,3]
    b = StreamArray()
    
    print(json.dumps([1,a,b]))
    

    Result is [1, [1, 2, 3], [20, 30, 40]].

提交回复
热议问题