我正在尝试使用以下代码构建JSON,但jsonlint表示它无效,以EOF错误结束。 错误是我必须分开输出,但我的目标是拥有一个JSON文件。
以下是我正在使用的代码:
t = 0
p = []
nome_b = (bollist[0])
p_causale = (causalestr) #contains a string
for item in jsonlist:
predizione = (jsonlist[t])
bollettini = {'Nome bollettino': str(nome_b)}
causale = {'Stringa Causale': str(p_causale)}
numero = {'Numero': str(predizione)}
t += 1
p.append([bollettini]+[causale]+[numero])
if not y:
jsontxt = ('Not found')
else:
#jsontxt = ([bollettini],[causale],[numero])
jsontxt = (p)
with open((TXT.txt'), 'a', encoding='utf-8') as outfile:
json.dump(jsontxt, outfile)
这是输出:
[[{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "0[(96.48 % n: 0) (3.37 % n: 9) (0.06 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "2[(36.17 % n: 2) (25.01 % n: 0) (18.48 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "6[(99.9 % n: 6) (0.09 % n: 4) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "5[(99.98 % n: 5) (0.01 % n: 3) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "4[(84.76 % n: 4) (7.63 % n: 0) (2.08 % n: 6)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "8[(89.88 % n: 8) (8.87 % n: 9) (1.11 % n: 5)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "9[(84.29 % n: 9) (8.23 % n: 8) (6.79 % n: 0)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "026554891"}, {"Numero": "1[(99.99 % n: 1) (0.0 % n: 9) (0.0 % n: 8)]"}]][[{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "4[(80.03 % n: 4) (18.44 % n: 1) (0.69 % n: 3)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "6[(99.91 % n: 6) (0.04 % n: 0) (0.03 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "0[(99.99 % n: 0) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "2[(99.86 % n: 2) (0.12 % n: 8) (0.01 % n: 3)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "9[(99.07 % n: 9) (0.83 % n: 8) (0.04 % n: 5)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "3[(99.99 % n: 3) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "1[(50.74 % n: 1) (40.73 % n: 7) (5.93 % n: 5)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "5[(36.81 % n: 5) (29.07 % n: 8) (16.1 % n: 6)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "6[(98.46 % n: 6) (0.66 % n: 4) (0.64 % n: 2)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "45602931566"}, {"Numero": "6[(69.49 % n: 6) (20.06 % n: 1) (6.35 % n: 8)]"}]][[{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "5[(60.71 % n: 5) (29.06 % n: 8) (8.77 % n: 7)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "7[(85.45 % n: 7) (8.71 % n: 9) (1.92 % n: 0)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "1[(99.99 % n: 1) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "1[(99.18 % n: 1) (0.42 % n: 2) (0.39 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "8[(99.98 % n: 8) (0.01 % n: 2) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "7[(99.8 % n: 7) (0.12 % n: 3) (0.04 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "6[(99.99 % n: 6) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "8[(59.18 % n: 8) (32.39 % n: 2) (6.08 % n: 6)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "1[(99.99 % n: 1) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "1[(95.61 % n: 1) (3.78 % n: 2) (0.48 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "5711587681155"}, {"Numero": "5[(99.85 % n: 5) (0.14 % n: 8) (0.0 % n: 9)]"}]][[{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "1[(75.32 % n: 1) (24.4 % n: 2) (0.26 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "8[(99.97 % n: 8) (0.01 % n: 2) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "7[(99.99 % n: 7) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "7[(64.8 % n: 7) (35.17 % n: 2) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "8[(97.63 % n: 8) (1.19 % n: 3) (1.14 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "5[(80.64 % n: 5) (18.03 % n: 3) (0.81 % n: 7)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "158775853"}, {"Numero": "3[(99.99 % n: 3) (0.0 % n: 9) (0.0 % n: 8)]"}]][[{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "1[(99.99 % n: 1) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "4[(80.31 % n: 4) (10.32 % n: 1) (5.49 % n: 2)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "1[(99.43 % n: 1) (0.31 % n: 2) (0.22 % n: 7)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "1[(92.47 % n: 1) (4.77 % n: 4) (2.64 % n: 6)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "5[(99.99 % n: 5) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "8[(99.42 % n: 8) (0.56 % n: 2) (0.01 % n: 3)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "8[(99.99 % n: 8) (0.0 % n: 9) (0.0 % n: 7)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "3[(98.3 % n: 3) (1.69 % n: 5) (0.0 % n: 9)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "3[(99.99 % n: 3) (0.0 % n: 9) (0.0 % n: 8)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "7[(98.05 % n: 7) (1.5 % n: 2) (0.42 % n: 4)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "0[(80.66 % n: 0) (18.98 % n: 2) (0.23 % n: 1)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "8[(31.61 % n: 8) (28.47 % n: 4) (23.53 % n: 6)]"}], [{"Nome bollettino": "BR_3_VE33491116420151103K000351FA.png"}, {"Stringa Causale": "1411588337082"}, {"Numero": "2[(66.61 % n: 2) (26.19 % n: 4) (4.39 % n: 8)]"}]]"Not Found"
如果您尝试validate it,则说它无效。 我怎样才能解决这个问题 ? 另外,如何将“Nome bollettino”作为其他对象的root用户? 我的意思是,像这样:
- Nome bollettino:
- Stringa Causale:
- Numero:
答案 0 :(得分:2)
您似乎在为文件添加单独的JSON结构,这很好 - 这是一个JSON line文件。但是,json.dump
不会在您的数据后自动添加换行符分隔符。你必须自己做。
with open(...) as outfile:
json.dump(jsontxt, outfile)
outfile.write('\n')
验证JSON时,请单独验证每一行。
如果要将JSON合并为一个,则会在循环中累积所有内容并在最后进行转储。它是这样的:
data = []
for item in jsonList:
... # do something
data.append(newItem) # newItem is your final json sub-structure
with open(..., 'w', encoding='utf-8') as f:
json.dump(data, f)
答案 1 :(得分:1)
您以附加模式'a'
打开输出文件,这意味着您有一个接一个的两个json位,结果不是一个json值。
您不能将json字符串附加到文本文件,并期望生成的文件是有效的json。改为以'w'
模式打开文件,您将覆盖旧值。