来自STDIN的Python JSON输入出现问题 [英] Trouble with Python JSON input from STDIN

查看:113
本文介绍了来自STDIN的Python JSON输入出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

input = json.load(sys.stdin)
print(input['id'])

当我输入{"id":1}并按Enter键时,我的程序无法继续,只是停留在输入中.将有效的json传递到我的stdlin后,如何使程序继续运行?

When I input {"id":1} and hit enter, my program does not continue, I am just stuck typing in my input. How can I make the program continue after valid json has been passed to my stdlin?

推荐答案

当您从sys.stdin读入时,它将读取所有内容,直到它碰到通常是ctrl-d的EOF字符为止,因此,如果您输入{"id":1} <ENTER> ctrl-d,它应该可以工作.

when you read in from sys.stdin it will read everything until it hits an EOF character normally ctrl-d so if you input {"id":1} <ENTER> ctrl-d it should work.

您似乎想做的事情就是这样

It looks like what you are trying to do is something like this

import json
json_as_str = input()
json_obj = json.loads(json_as_str)
print(json_obj['id'])

这篇关于来自STDIN的Python JSON输入出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆