Trouble with Python JSON input from STDIN

混江龙づ霸主 提交于 2019-12-11 04:47:51

问题


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

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?


回答1:


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'])


来源:https://stackoverflow.com/questions/50180360/trouble-with-python-json-input-from-stdin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!