All the Things about IT & US stocks
TIL(Today I learned)

TIL #1

파이썬으로 json 데이터 처리하기

json.loads(): parse a valid JSON string and convert it into a Python Dictionary.
json.loads(json_data)

json.dump(): The dump() method is used when the Python objects have to be stored in a file.
json.dump(json_data, file, indent=4)

내가 코드에서 활용한 부분:


with urllib.request.urlopen("http://sugarmate.io/api/v1/nddm7x/latest.json") as url:
data = url.read()
j = json.loads(data)
if os.path.isfile("sugarmate.json"+time.strftime('%Y-%m-%d', time.localtime(time.time()))):
with open("sugarmate.json_"+time.strftime('%Y-%m-%d', time.localtime(time.time())),"a") as f:
json.dump(j, f, indent=4)
else:
with open("sugarmate.json_"+time.strftime('%Y-%m-%d', time.localtime(time.time())),"w") as f:
json.dump(j, f)

Reference:
https://www.geeksforgeeks.org/json-dumps-in-python/

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

back to top