使用Python将双内容写入JSON
我是Python编程新手,所以如果我在任何地方犯了错误,请容忍我 我正在尝试使用2个字典编写一个json文件,并在Windows上使用以下代码将输出转储到该文件中使用Python将双内容写入JSON,python,json,Python,Json,我是Python编程新手,所以如果我在任何地方犯了错误,请容忍我 我正在尝试使用2个字典编写一个json文件,并在Windows上使用以下代码将输出转储到该文件中 import json import sys import string from time import strftime scan_results = open("scan_results.txt", "r") saved = sys.stdout f = file('report.json', 'wb') sys.stdout
import json
import sys
import string
from time import strftime
scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
for line in scan_results:
if ".jpg" in line:
lst = []
result = line.split('\\')
result_split = result[5].split(' ')
filename = result_split[0]
raw_status = result_split[3]
if "OK" in raw_status:
status = "Okay"
status_code = "0"
dict = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status}
dict2 = {filename : dict}
print json.dumps(dict2)
sys.stdout = saved
f.close()
print "JSON report written"
问题是,我的输出是
{
"car-30537.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30537.jpg"
}
}{
"car-30538.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30538.jpg"
}
}
而我想要的输出是
{
"car-30537.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30537.jpg"
},
{
"car-30538.jpg": {
"statusCode": "0",
"DateTime": "2012-02-07 09:52:26",
"Description": "Okay",
"FileName": "car-30538.jpg"
}
}
有什么方法可以解决这个问题吗?提前感谢您正在制作大量的dict,而您只需要一个包含以下内容的main:
import json
import sys
import string
from time import strftime
scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
dict2 = {} #Create one output dict
for line in scan_results:
if ".jpg" in line:
lst = []
result = line.split('\\')
result_split = result[5].split(' ')
filename = result_split[0]
raw_status = result_split[3]
if "OK" in raw_status:
status = "Okay"
status_code = "0"
dict2[filename] = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status} #Add to that dict.
print json.dumps(dict2) #Print it out at the end.
sys.stdout = saved
f.close()
print "JSON report written"
我在修改过的行中添加了注释。您正在制作大量的dict,而您只需要一个包含一个的main:
import json
import sys
import string
from time import strftime
scan_results = open("scan_results.txt", "r")
saved = sys.stdout
f = file('report.json', 'wb')
sys.stdout = f
dict2 = {} #Create one output dict
for line in scan_results:
if ".jpg" in line:
lst = []
result = line.split('\\')
result_split = result[5].split(' ')
filename = result_split[0]
raw_status = result_split[3]
if "OK" in raw_status:
status = "Okay"
status_code = "0"
dict2[filename] = {'FileName': filename, 'DateTime': strftime("%Y-%m-%d %H:%M:%S"), 'statusCode': status_code, 'Description': status} #Add to that dict.
print json.dumps(dict2) #Print it out at the end.
sys.stdout = saved
f.close()
print "JSON report written"
我在修改后的行中添加了注释。您好,谢谢您的快速回复。它仍然给我第一个输出,正如我在文章中提到的question@androidnoob你确定吗?这似乎不对。你能把你的数据文件给我(把它添加到你的问题中)让我测试一下吗?哎呀,我在仔细检查后把
print json.dumps(dict2)
放在了错误的缩进位置。非常感谢你的帮助!这个代码非常疯狂。(1) lst
已写入但从未读取(2)状态始终正常,状态代码始终为0(3)没有修改系统的充分理由。stdout(4)代码应打开读取,然后创建dict,然后打开写入。这三者兼而有之。(5) 未使用字符串导入(6)打开文件时使用两种习惯用法--file()和open()。。。。据我所知,这将做同样的事情,而且会更干净:@hughdbrown完全同意这一点-我只是按照要求做了必要的更改,没有做任何其他更改。嗨,谢谢你的快速回复。它仍然给我第一个输出,正如我在文章中提到的question@androidnoob你确定吗?这似乎不对。你能把你的数据文件给我(把它添加到你的问题中)让我测试一下吗?哎呀,我在仔细检查后把print json.dumps(dict2)
放在了错误的缩进位置。非常感谢你的帮助!这个代码非常疯狂。(1) lst
已写入但从未读取(2)状态始终正常,状态代码始终为0(3)没有修改系统的充分理由。stdout(4)代码应打开读取,然后创建dict,然后打开写入。这三者兼而有之。(5) 未使用字符串导入(6)打开文件时使用两种习惯用法--file()和open()。。。。据我所知,这将做同样的事情,而且会更干净:@hughdbrown完全同意这一点——我只是按照要求做了必要的更改,使其正常工作,我没有做任何其他更改。