Convert a list to json objects

后端 未结 4 1650
我寻月下人不归
我寻月下人不归 2020-12-17 03:46

I have a huge text file.

line 1
line 2
line 3
...

I have converted it into an array of lists:

[[\'String 1\'],[\'String 2\'         


        
相关标签:
4条回答
  • 2020-12-17 04:10

    As @alecxe pointed out, you need to divide the array of lists you got from the file into groups of values with 7 or fewer elements. You can then take a list of any 7 titles you want and use them as keys to create the dictionary of each json object in the final list.

    try:
        from itertools import izip
    except ImportError:  # Python 3
        izip = zip
    
    try:
        xrange
    except NameError:  # Python 3
        xrange = range
    
    def grouper(n, sequence):
        for i in xrange(0, len(sequence), n):
            yield sequence[i:i+n]
    
    data = [['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
            ['String 6'],['String 7'],['String 8'],['String 9'],['String 9'],
            ['String 10']]
    
    titles = ['title1', 'title2', 'title3', 'title4', 'title5', 'title6', 'title7']
    
    values = [e[0] for g in grouper(7, data) for e in g]
    keys = (titles[i%7] for i in xrange(len(values)))
    
    objs = [dict(g) for g in grouper(7, list(izip(keys, values)))]
    print(objs)
    

    Output:

    [{'title1': 'String 1', 'title2': 'String 2', 'title3': 'String 3',
      'title4': 'String 4', 'title5': 'String 5', 'title6': 'String 6',
      'title7': 'String 7'}, {'title1': 'String 8', 'title2': 'String 9',
      'title3': 'String 9', 'title4': 'String 10'}]
    
    0 讨论(0)
  • 2020-12-17 04:17

    Define a class as custom type before serializing. Then set this in a loop in the main class and return using json.dumps()

    import json
    
    class CustomType:
        def __init__(self, title, text):
            self.title = title
            self.text = text
    
        def toJSON(self):
            '''
            Serialize the object custom object
            '''
            return json.dumps(self, default=lambda o: o.__dict__, sort_keys=True, indent=4)
    

    in main class:

    def get_json_data():
        '''
        Convert string array to json array
        '''
        result = []
        for item in data:
            obj = CustomType("title(n)",item)
            result.append(json.loads(obj.toJSON()))
    
        return json.dumps(result)
    
    0 讨论(0)
  • 2020-12-17 04:22

    To solve this, you need to split the input list into chunks, by 7 in your case. For this, let's use this approach. Then, use a list comprehension producing a list of dictionaries:

    >>> from pprint import pprint
    >>> l = [['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
    ... ['String 6'],['String 7'],['String 8'],['String 9'],['String 10'],
    ... ['String 11']]
    >>> def chunks(l, n):
    ...     """Yield successive n-sized chunks from l."""
    ...     for i in range(0, len(l), n):
    ...         yield l[i:i+n]
    ... 
    >>>
    >>> result = [{"title%d" % (i+1): chunk[i][0] for i in range(len(chunk))} 
                  for chunk in chunks(l, 7)]
    >>> pprint(result)
    [{'title1': 'String 1',
      'title2': 'String 2',
      'title3': 'String 3',
      'title4': 'String 4',
      'title5': 'String 5',
      'title6': 'String 6',
      'title7': 'String 7'},
     {'title1': 'String 8',
      'title2': 'String 9',
      'title3': 'String 10',
      'title4': 'String 11'}]
    
    0 讨论(0)
  • 2020-12-17 04:29

    Just adding onto alexce's response, you can easily convert the restructured data into JSON:

    import json
    json.dumps(result)
    

    There are some potential security concerns with top-level arrays. I'm not sure if they're still valid with modern browsers, but you may want to consider wrapping it in an object.

    import json
    json.dumps({'results': result})
    
    0 讨论(0)
提交回复
热议问题