如何使用python创建字典列表 [英] how to create a list of dictionaries using python
问题描述
我有一个函数,该函数返回一个列表,在该列表中我序列化为json对象并将其写入JSON文件.
I have a function that returns a list where I serialized into a json object and write it to a JSON file.
结果是正确的,但是问题在于它返回了分隔列表中的每条记录.
the result is correct but the problem is that it returns each record in the separated list.
我想要的是返回一个包含多个词典项目的列表.
what I want is to return one list with multiple dictionary items.
[{"file Name": "test1.txt", "searched Word": "th", "number of occurence": 1}][{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]
预期结果
[
{
"file Name": "test1.txt",
"searched Word": "th",
"number of occurence": 1
}
{
"file Name": "test2.txt",
"searched Word": "th",
"number of occurence": 1
}
]
我该如何实现?
for counter, myLine in enumerate(textList):
thematch=re.sub(searchedSTR,RepX,myLine)
matches = re.findall(searchedSTR, myLine, re.MULTILINE | re.IGNORECASE)
if len(matches) > 0:
# add one record for the match (add one because line numbers start with 1)
d[matches[0]].append(counter + 1)
self.textEdit_PDFpreview.insertHtml(str(thematch))
'''
loop over the selected file and extract 3 values:
==> name of file
==> searched expression
==> number of occurence
'''
listMetaData=[]
for match, positions in d.items():
listMetaData.append({"file Name":fileName,"searched Word":match,"number of occurence":len(positions)})
jsondata = json.dumps(listMetaData)
print(jsondata)
推荐答案
下面是一个示例代码,用于将多个词典列表转换为单个词典列表.
Here is a sample code to convert multiple lists of dictionaries into single list of dictionaries.
我假设您的json逗号分隔,即每个列表之间都有逗号.
I assume your json has comma separated i.e there is comma between each list.
[{"file Name": "test1.txt", "searched Word": "th", "number of occurence": 1}],[{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]
示例代码:
myDict = []
myJsonvar = [{"file Name": "test1.txt", "searched Word": "th", "number of occurence": 1}],[{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]
for idx,jstrings in enumerate(myJsonvar) :
for inx, elem in enumerate(jstrings) :
myDict.append(myJsonvar[idx][inx])
print(myDict)
输出:
[{'file Name': 'test1.txt', 'searched Word': 'th', 'number of occurence': 1}, {'file Name': 'test2.txt', 'searched Word': 'th', 'number of occurence': 1}]
此输出还将是逗号分隔的单列表字典.
This output will also be comma separated single list dictionaries.
其他方法: 如果您不想制作字典列表或不想在列表之间添加逗号.
Other approach : if you don't want to make list of dictionaries or append comma between lists .
您可以尝试以下代码:
import re
import yaml
myListOfDict = []
myJsonvar = '[{"file Name": "test1.txt", "searched Word": "th", "number of occurence": 1}][{"file Name": "test2.txt", "searched Word": "th", "number of occurence": 1}]'
mylist = myJsonvar.split("][")
for lst in mylist :
updateVar = re.sub(r'[\[\]]', '',lst)
myListOfDict.append(yaml.load(updateVar))
print(myListOfDict)
输出
[{'file Name': 'test1.txt', 'searched Word': 'th', 'number of occurence': 1}, {'file Name': 'test2.txt', 'searched Word': 'th', 'number of occurence': 1}]
希望这对您有帮助
这篇关于如何使用python创建字典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!