JSON (JavaScript Object Notation)
-๋์
๋๋ฆฌ ๋น์ทํ๋ค
-๊ตฌ์กฐ { K : V }
1. Json Writer
# ์ฒซ ๋ฒ์งธ ๋ฐฉ๋ฒ
dict_list = [
{
'name' : 'james',
'age':20,
'spec':[
175.5,
70.5
]
},
{
'name':'alice',
'age':21,
'spec': [
168.5,
60.5
]
}
]
json_string = json.dumps(dict_list)
with open('dictlist.json', 'w') as file:
file.write(json_string)
print('dictlist.json ํ์ผ์ด ์์ฑ๋์์ต๋๋ค.')
# ๋ ๋ฒ์งธ ๋ฐฉ๋ฒ
import json
dict_list = [
{
'name' : 'james',
'age':20,
'spec':[
175.5,
70.5
]
},
{
'name':'ํ๊ธธ๋',
'age':21,
'spec': [
168.5,
60.5
]
}
]
# indent ๋ค์ฌ์ฐ๊ธฐ, ensure_ascii=False ํ๊ธ์ฝ๊ธฐ
json_string = json.dumps(dict_list, indent=4, ensure_ascii=False)
with open('dictlist.json', 'w', encoding='UTF-8') as file:
file.write(json_string)
print('dictlist.json ํ์ผ์ด ์์ฑ๋์์ต๋๋ค.')
2. Json Reader
import json
with open('dictList.json', 'r', encoding='UTF-8') as file:
json_reader = file.read()
dict_list = json.loads(json_reader)
for dic in dict_list:
print('์ด๋ฆ: {}'.format(dic['name']))
print('๋์ด: {}'.format(dic['age']))
print('ํค: {}'.format(dic['spec'][0]))
print('๋ชธ๋ฌด๊ฒ: {}'.format(dic['spec'][1]))
print()
'๐์น ๊ฐ๋ฐ(Web) > ๐ํ์ด์ฌ(Python)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ์ด์ฌ ์น ํฌ๋กค๋ง ์ ์ ํฌ๋กค๋ง selenium ๋ง์ฐ์ค ์ ์ด ํค๋ณด๋ ์ ์ด selenium๊ณผ bs4์ ์กฐํฉ (1) | 2023.05.10 |
---|---|
ํ์ด์ฌ ์น ํฌ๋กค๋ง Beautiful Soup (2) | 2023.05.09 |
ํ์ผ๋ณต์ฌ/csvํ์ผ ์ฝ๊ธฐ ์ฐ๊ธฐ (0) | 2023.02.08 |
ํ์ผ ๋ง๋ค๊ณ ์ฝ๊ธฐ (0) | 2023.02.06 |
์ง์ญ๋ณ์(local) ์ ์ญ๋ณ์(global) (0) | 2023.02.01 |