我有一个巨大的文本文件。
line 1
line 2
line 3
...
我已将其转换为列表数组:
[['String 1'],['String 2'],['String 3'],['String 4'],['String 5'],
['String 6'],['String 7'],['String 8'],['String 9'],['String 9'],
['String 10'], ...]
我想将此列表转换为JSON对象,如下所示:
[{'title1': 'String 1', 'title2': 'String 2', ... , 'title7': 'String 7'},
{'title1': 'String 8', ..., 'title7': 'String 14'}, ...]
我不知道该怎么做。任何帮助。?
答案 0 :(得分:6)
只需添加到alexce的响应,您就可以轻松地将重组后的数据转换为JSON:
import json
json.dumps(result)
有一些潜在的security concerns顶级数组。我不确定它们是否仍适用于现代浏览器,但您可能需要考虑将其包装在对象中。
import json
json.dumps({'results': result})
答案 1 :(得分:4)
要解决此问题,您需要将输入列表拆分为块,在您的情况下为7。为此,请使用this approach。然后,使用 list comprehension 生成字典列表:
>>> 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'}]
答案 2 :(得分:1)
正如@alecxe指出的那样,你需要将你从文件中获得的列表数组分成7个或更少元素的值组。然后,您可以获取所需的任何7个标题的列表,并将它们用作键,以在最终列表中创建每个json对象的字典。
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)
输出:
[{'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'}]
答案 3 :(得分:0)
在序列化之前将类定义为自定义类型。然后在主类的循环中设置它并使用json.dumps()
返回<div class="parent">
<p>The Parent paragraph.</p>
<div class="child">
<p>This is Child Para</p>
<div class="parent">
<p>The Parent paragraph.</p>
<div class="child">
<p>This is Child Para</p>
<div class="parent">
<p>The Parent paragraph.</p>
<div class="child">
<p>This is Child Para</p>
</div>
</div>
</div>
</div>
</div>
<div>This is second Para</div>
</div>
主要课程中的:
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)