我需要根据用户输入创建一个json文件,也就是说,如果给出了输入,则应该将其存储在json文件中。像这样,它需要存储来自user的50个输入。我尝试了我的代码,但是打印json文件中的最后一个值。
正在使用的代码是:
import json
a=0
b=0
while True:
a=input('enter the value for a')
b=input('enter the value for b')
print(a,b)
with open('som.json', 'w') as f:
json.dump(a,f)
json.dump(b,f)
打印输出为:
>>> %Run stack.py
enter the value for a1
enter the value for b2
1 2
enter the value for a7
enter the value for b8
7 8
enter the value for a5
enter the value for b6
5 6
enter the value for a1
enter the value for b3
1 3
enter the value for a2
enter the value for b3
2 3
enter the value for a8
enter the value for b9
8 9
enter the value for a5
enter the value for b6
5 6
enter the value for a2
enter the value for b3
2 3
enter the value for aTraceback (most recent call last):
File "/home/pi/stack.py", line 8, in <module>
a=input('enter the value for a')
KeyboardInterrupt: Execution interrupted
>>>
但输出的json是:
"2""3"
告诉我如何将这些json文件转换为图形格式。