所以我的json数据看起来像这样:
"responses":[
{
"ResponseID" : "R_1mhpDCQzIOlVfPT",
"ResponseSet" : "Default Response Set",
"IPAddress" : "",
"StartDate" : "2016-08-04 11:52:36",
"EndDate" : "2016-08-04 11:52:53",
"RecipientLastName" : "",
"RecipientFirstName" : "",
"RecipientEmail" : "",
"ExternalDataReference" : "",
"Finished" : "1",
"Status" : "1",
"Q5" : "",
"Q6" : "",
"Q7" : "",
"Q8" : "",
"Q9" : "",
"Q10" : "",
"Q11" : "",
"Q12" : "",
"LocationLatitude" : "33.414794921875",
"LocationLongitude" : "-111.90930175781",
"LocationAccuracy" : "-1"
},
我基本上想要把所有的Q都放在json中的Questions数组中。输出应该如下所示:
"responses":[
{
"ResponseID" : "R_1mhpDCQzIOlVfPT",
"ResponseSet" : "Default Response Set",
"IPAddress" : "",
"StartDate" : "2016-08-04 11:52:36",
"EndDate" : "2016-08-04 11:52:53",
"RecipientLastName" : "",
"RecipientFirstName" : "",
"RecipientEmail" : "",
"ExternalDataReference" : "",
"Finished" : "1",
"Status" : "1",
"Questions" : [
"Q5" : "",
"Q6" : "",
"Q7" : "",
"Q8" : "",
"Q9" : "",
"Q10" : "",
"Q11" : "",
"Q12" : ""
],
"LocationLatitude" : "33.414794921875",
"LocationLongitude" : "-111.90930175781",
"LocationAccuracy" : "-1"
}
我怎么能解决这个问题并将其应用于100多个回复。以下是我到目前为止的情况:
for filename in os.listdir('C:/Users/john/Desktop/Q/QD'):
if filename.endswith(".json") :
print(filename)
with open(filename, encoding="utf8") as data_file:
data = json.load(data_file)
for i in data['responses']:
for j in i:
if j.startswith('Q'):
print(j)
input("Press enter to continue...")
所有这些代码都是加载数据并基本上循环遍历文件夹中的每个文件,并将所有问题打印到控制台中。我如何附加问题字段并添加方括号?
答案 0 :(得分:5)
这是一个有效的例子:
for filename in os.listdir('C:/Users/john/Desktop/Q/QD'):
if filename.endswith(".json"):
with open(filename, encoding="utf8") as data_file:
data = json.loads(data_file)
for response in data['responses']:
questions = {}
for key in list(response.keys()):
if key.startswith('Q'):
questions[key] = response[key]
del response[key]
response['Questions'] = questions
print(response)
很少注意到:
list(response.keys())
生成密钥的副本,如果没有,del
稍后会在您迭代时更改dict时出错。questions
字典中,并在稍后的回复中显示。startswith
可能会导致你的问题,当键开始于" Q"喜欢"数量"等答案 1 :(得分:0)
我是Python的菜鸟,不得不首先与验证JSON进行斗争,但是这样会有用吗?
给定输入:
{
"responses": [{
"ResponseID": "R_1mhpDCQzIOlVfPT",
"ResponseSet": "Default Response Set",
"IPAddress": "",
"StartDate": "2016-08-04 11:52:36",
"EndDate": "2016-08-04 11:52:53",
"RecipientLastName": "",
"RecipientFirstName": "",
"RecipientEmail": "",
"ExternalDataReference": "",
"Finished": "1",
"Status": "1",
"Q5": "",
"Q6": "",
"Q7": "",
"Q8": "",
"Q9": "",
"Q10": "",
"Q11": "",
"Q12": "",
"LocationLatitude": "33.414794921875",
"LocationLongitude": "-111.90930175781",
"LocationAccuracy": "-1"
}]
}
使用此代码:
import json
def main():
f = open('test.json')
a = json.load(f)
print a.keys()
qs = []
for k in a[u'responses'][0]:
if 'Q' in k:
qs.append((k, a[u'responses'][0][k]))
del k
a[u'responses'][0]['questions'] = qs
print a
if __name__ == "__main__":
main()
给出这个输出:
{u'responses': [{u'Q5': u'', u'Q7': u'', u'Q6': u'', u'Q9': u'', u'Q8': u'', u'ResponseID': u'R_1mhpDCQzIOlVfPT', u'LocationLatitude': u'33.414794921875', u'RecipientLastName': u'', 'questions': [(u'Q5', u''), (u'Q7', u''), (u'Q6', u''), (u'Q9', u''), (u'Q8', u''), (u'Q11', u''), (u'Q10', u''), (u'Q12', u'')], u'Status': u'1', u'StartDate': u'2016-08-04 11:52:36', u'EndDate': u'2016-08-04 11:52:53', u'RecipientEmail': u'', u'Finished': u'1', u'Q11': u'', u'Q10': u'', u'Q12': u'', u'IPAddress': u'', u'RecipientFirstName': u'', u'LocationAccuracy': u'-1', u'LocationLongitude': u'-111.90930175781', u'ExternalDataReference': u'', u'ResponseSet': u'Default Response Set'}]}
您需要根据自己的使用情况进行调整。
我顺便使用Python 2.7。说实话,以前的解决方案看起来更优雅。我发布了更多信息来尝试帮助和学习。随意指出我的错误!