我有一个列表,我想对其进行迭代并将其写入以写入json。 例如:
lis_1 = ['fellow','hello','yellow']
现在我将其存储为字典:
items_main = {}
new_list = []
result_2 = {}
items = {}
lis_1 = ['fellow','hello','yellow']
for i,l in enumerate(lis_1):
key, value = str(i+1), str(lis_1[i])
items[key] = value
new_list.append(items)
for d in new_list:
result_2.update(d)
print(result_2)
我也是同一个变量
name = 'buddy'
year = 2008
sample = {
"FDRID1":{
"1": {
"Property_name":{
"1": str(name )
},
items_main['Building Highlights'] = result_2
"Year":{
"1":str(year)
}
}
}
}
with open(out_file, 'w') as fp:
json.dump(sample, fp)
预期输出:
"FDRID1":{
"1": {
"Property_name":{
"1": "buddy"
},
"BuildingHighlights":{
"1":"fellow",
"2":"hello",
"3":"yellow"
},
"Year":{
"1":'2008'
}
}
}
如果** lis_1 = [''] **也就是(空)。输出必须为:
"FDRID1":{
"1": {
"Property_name":{
"1": "buddy"
},
"BuildingHighlights":{
"1":"",
"
},
"Year":{
"1":'2008'
}
}
}
答案 0 :(得分:0)
我不太确定我是否完全了解您要执行的操作:
void print (int * begin, int * end){
while(begin != end){
printf("%d", *begin);
begin++;
}
}
您的空列表示例输出不是有效的json,因为它具有虚假的result_1 = {str(i): v for i, v in enumerate(lis_1, 1)} if lis_1 else {'1', ''}
-假定这只是一个编辑错误。