简体   繁体   English

如何通过python字典循环保存每个输出数据

[英]How to save every output data in loop by python dictionary

Today i found problem in my python dict. 今天,我在python字典中发现了问题。 I need to collect all output data from loop and write it in json file. 我需要从循环中收集所有输出数据并将其写入json文件中。 Process of writing works well, but, as result, inside json file only one, last output. 编写过程效果很好,但是结果是,在json文件中只有一个,最后一个输出。 How i should fix part of my code with dictionary? 我应该如何用字典修复部分代码? Here is code: 这是代码:

def classify(sess, label_list, softmax_output, coder, images, image_file):

print('Running file %s' % image_file)
image_batch = make_batch(image_file, coder, not FLAGS.single_look)
batch_results = sess.run(softmax_output, feed_dict={images:image_batch.eval()})
output = batch_results[0]
batch_sz = batch_results.shape[0]
for i in range(1, batch_sz):
    output = output + batch_results[i]

output /= batch_sz
best = np.argmax(output)
best_choice = (label_list[best], output[best])
Guess = 'Guess @ 1 %s, prob = %.2f' % best_choice
print('Guess @ 1 %s, prob = %.2f' % best_choice)
faces_pred = dict()
faces_pred =  {"face_pred": Guess}
with open('pred.json', 'w') as fp:
    json.dump( faces_pred, fp)

nlabels = len(label_list)
if nlabels > 2:
    output[best] = 0
    second_best = np.argmax(output)

    print('Guess @ 2 %s, prob = %.2f' % (label_list[second_best], output[second_best]))
return best_choice

I am interesting only in all output's of Guess 1! 我只在Guess 1的所有输出中都很有趣! Thank you for your time :3 谢谢您的时间:3

UPD--------- Inside json file, after run my program, have this output: UPD ---------在json文件中,运行我的程序后,输出如下:

{"face_pred": "Guess @ 1 (60, 100), prob = 0.75"}

That's correct, but only for one face (i have 3+ Guess @ 1). 没错,但仅适用于一张脸(我的猜测值为1,我的猜测为3+)。 It's not adding new data in json file, just rewriting it! 它不是在json文件中添加新数据,而只是重写它!

UPD2-------- How it looks like is console: UPD2 --------控制台的外观:

  • Running file ./frontal-face-1.jpg 运行文件./frontal-face-1.jpg
  • Running multi-cropped image 运行多幅图像
  • Guess @ 1 (60, 100), prob = 0.95 猜测@ 1(60,100),概率= 0.95
  • Guess @ 2 (4, 6), prob = 0.02 猜测@ 2(4,6),概率= 0.02
  • Running file ./frontal-face-2.jpg 运行文件./frontal-face-2.jpg
  • Running multi-cropped image 运行多幅图像
  • Guess @ 1 (60, 100), prob = 0.75 猜测@ 1(60,100),概率= 0.75
  • Guess @ 2 (38, 43), prob = 0.12 猜测@ 2(38,43),概率= 0.12
  • ubuntu@ubuntu122:~/roman/IWAdector$ ubuntu @ ubuntu122:〜/ roman / IWAdector $

with your comments i now understand your problem... you open your file like this: 有了您的评论,我现在了解您的问题...您像这样打开文件:

with open('pred.json', 'w') as fp:
    json.dump( faces_pred, fp)

and that rewrites the entire file each time, so you only get the last result there 并且每次都会重写整个文件,因此您只能在那里得到最后的结果

you could do this instead: 您可以改为:

with open('pred.json', 'a') as fp:
    data_to_write = json.dumps(faces_pred)
    fp.write(faces_pred + '\n')

notice 2 things i did here: 注意我在这里做的两件事:

  1. I opened the file with the 'a' flag and not 'w', that is used for appending to the end of the file each time instead of rewriting the file 我用“ a”标志而不是“ w”打开文件,该标志用于每次附加到文件末尾而不是重写文件

  2. I used json.dumps instead of json.dump to produce a string, and then wrote that string (appended it) to the file, with \\n at the end to ensure it will break the line after each time 我使用json.dumps而不是json.dump来生成一个字符串,然后将该字符串(附加在字符串中)写入文件中,并以\\n结尾,以确保每次之后都将其换行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM