본문 바로가기
파이썬

파이썬, 파일에 Json 쓰기

by º기록 2021. 1. 17.
반응형

첫 번째 json 파일을 작성하려고합니다. 그러나 어떤 이유로 실제로 파일을 쓰지 않습니다. 덤프를 실행 한 후에는 파일에 넣은 임의의 텍스트가 지워지지 만 그 자리에는 아무것도 없기 때문에 뭔가를하고 있다는 것을 알고 있습니다. 말할 것도없이로드 부분은 거기에 아무것도 없기 때문에 오류가 발생합니다. 이것은 파일에 모든 json 텍스트를 추가해야하지 않습니까?

from json import dumps, load
n = [1, 2, 3]
s = ["a", "b" , "c"]
x = 0
y = 0

with open("text", "r") as file:
    print(file.readlines())
with open("text", "w") as file:
    dumps({'numbers':n, 'strings':s, 'x':x, 'y':y}, file, indent=4)
file.close()

with open("text") as file:
    result = load(file)
file.close()
print (type(result))
print (result.keys())
print (result)

 

해결 방법

 


with open("text", "w") as outfile:
    json.dump({'numbers':n, 'strings':s, 'x':x, 'y':y}, outfile, indent=4)

 

참조 페이지 https://stackoverflow.com/questions/16267767

 

 

반응형

댓글