๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/Python

[Python]ํŒŒ์ด์ฌ์œผ๋กœ CSV์™€ JSON ํŒŒ์ผ ๋‹ค๋ฃจ๊ธฐ: ์ž…์ถœ๋ ฅ ๊ฐ€์ด๋“œ

by YJ Dev 2024. 4. 24.
728x90
๋ฐ˜์‘ํ˜•
SMALL

๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๋ฐ ํŒŒ์ผ ์ž…์ถœ๋ ฅ์€ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ํŠนํžˆ CSV์™€ JSON์€ ๋ฐ์ดํ„ฐ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์ €์žฅํ•˜๊ณ  ๊ตํ™˜ํ•˜๋Š” ๋ฐ ๋„๋ฆฌ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŠธ์—์„œ๋Š” python์„ ์‚ฌ์šฉํ•˜์—ฌ CSV์™€ JSON ํŒŒ์ผ์„ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

csv json python


1. csv ํŒŒ์ผ ์ž…์ถœ๋ ฅ๐Ÿ“‚

csv ํŒŒ์ผ์€ ํ…์ŠคํŠธ ๊ธฐ๋ฐ˜์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ–‰๊ณผ ์—ด๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ €์žฅํ•˜๋Š” ํ˜•์‹์ž…๋‹ˆ๋‹ค. csv ํŒŒ์ผ์„ ์ฝ์€ ํ›„ ํŒŒ์ผ ์•ˆ์— ์žˆ๋Š” ๋‚ด์šฉ์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

# csv ํŒŒ์ผ ์ฝ๊ธฐ
student_list = []  # ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค

# './csvfile/ํ•™์ƒ๋ช…๋‹จ.csv' ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('rt')๋กœ ์—ด๊ณ  'file' ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค
with open('./csvfile/ํ•™์ƒ๋ช…๋‹จ.csv', 'rt') as file:
    file.readline()  # ์ƒ๋‹จ์˜ ์ œ๋ชฉ ์ค„์„ ์ฝ์–ด ๊ฑด๋„ˆ๋›ด๋‹ค
    while True:  # ๋ฌดํ•œ ๋ฃจํ”„ ์‹œ์ž‘
        line = file.readline()  # ํ•œ ์ค„์”ฉ ์ฝ์–ด line ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค
        if not line:  # ๋งŒ์•ฝ ๋” ์ด์ƒ ์ฝ์„ ๋ผ์ธ์ด ์—†๋‹ค๋ฉด
            break  # ๋ฃจํ”„๋ฅผ ์ข…๋ฃŒํ•œ๋‹ค
        student = line.split(',')  # ,๋กœ ๊ตฌ๋ถ„๋œ ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ 'student'์— ์ €์žฅํ•œ๋‹ค
        student_list.append(student)  # ๋ถ„๋ฆฌํ•œ ํ•™์ƒ ์ •๋ณด๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค

print(student_list)  # ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค

CSV
์œ„ ์ฝ”๋“œ์—์„œ ์ฝ์€ CSV ํŒŒ์ผ์˜ ๋‚ด์šฉ


๋จผ์ € ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์— ์žˆ๋Š” csv ํŒŒ์ผ์„ ์—ด๊ณ , csv ํŒŒ์ผ์˜ ์ฒซ ๋ฒˆ์งธ ์ค„(์ œ๋ชฉ ์ค„)์„ ๊ฑด๋„ˆ๋›ด ํ›„ ๋‚˜๋จธ์ง€ ๋‚ด์šฉ์„ ์ฝ์–ด์™€์„œ ๊ฐ ์ค„์„ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ๋กœ ์–ป์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

ํŒŒ์ด์ฌ์—์„œ ํŒŒ์ผ์„ ์ฝ์–ด์˜ค๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด ์ง€๋‚œ ํŒŒ์ผ ์ž…์ถœ๋ ฅ ํฌ์ŠคํŒ…์„ ํ™•์ธํ•ด ์ฃผ์„ธ์š”๐Ÿ˜

" "

[Pytohon]ํŒŒ์ด์ฌ ํŒŒ์ผ ์ž…๋ ฅ ์ถœ๋ ฅ: ๋ฐ์ดํ„ฐ ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ์˜ ๊ธฐ์ดˆ

๋ชฉ์ฐจ1. ํŒŒ์ผ ์ƒ์„ฑ๊ณผ ๋ฌธ์ž์—ด ์“ฐ๊ธฐ2. ํŒŒ์ผ์—์„œ ๋ฌธ์ž์—ด ์ฝ๊ธฐ3. ํŒŒ์ผ์—์„œ ์—ฌ๋Ÿฌ ์ค„์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ฝ๊ธฐ4. with ํ‚ค์›Œ๋“œ ์ด์šฉํ•œ ํŒŒ์ผ ์ž…์ถœ๋ ฅ5. ํŒŒ์ผ ์ž…์ถœ๋ ฅ ํ™œ์šฉ6. ํ•ต์‹ฌ ๋‚ด์šฉํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ผ์—

creativevista.tistory.com


ํŒŒ์ด์ฌ์—์„œ๋Š” csv
๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ csv ํŒŒ์ผ์„ ์‰ฝ๊ฒŒ ์ฝ๊ณ  ์“ธ ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ ๋™์ผํ•œ csv ํŒŒ์ผ์„ csv ๋ชจ๋“ˆ์„ ์ด์šฉํ•˜์—ฌ ์ฝ๊ณ  ์“ฐ๋Š” ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

import csv

# csv ํŒŒ์ผ ์ฝ๊ธฐ
student_list = []  # ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค

# './csvfile/ํ•™์ƒ๋ช…๋‹จ.csv' ๊ฒฝ๋กœ์˜ ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('rt')๋กœ ์—ด๊ณ  'file' ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค
with open('./csvfile/ํ•™์ƒ๋ช…๋‹จ.csv', 'rt', newline='') as file:
    csv_reader = csv.reader(file)  # CSV ๋ฆฌ๋” ๊ฐ์ฒด ์ƒ์„ฑ
    next(csv_reader)  # ์ฒซ ๋ฒˆ์งธ ์ค„(์ œ๋ชฉ ์ค„)์„ ๊ฑด๋„ˆ๋›ด๋‹ค
    for line in csv_reader:  # CSV ํŒŒ์ผ์˜ ๊ฐ ์ค„์— ๋Œ€ํ•ด ๋ฐ˜๋ณต
        student_list.append(line)  # ๊ฐ ์ค„์„ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค

# csv ํŒŒ์ผ ์“ฐ๊ธฐ (๋ฐ์ดํ„ฐ ์ถ”๊ฐ€)
new_data = ["์„ธ์ข…๋Œ€์™•", "010-6666-6666"]

# ์ถ”๊ฐ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€
student_list.append(new_data)

# ๊ฒฐ๊ณผ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
print(student_list)

2. jsonํŒŒ์ผ ์ž…์ถœ๋ ฅ๐Ÿ“

JSON ํŒŒ์ผ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํ…์ŠคํŠธ๋กœ ํ‘œํ˜„ํ•˜๋Š” ๊ฒฝ๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ ๊ตํ™˜ ํ˜•์‹์ž…๋‹ˆ๋‹ค. ํŒŒ์ด์ฌ์—์„œ๋Š” json ๋ชจ๋“ˆ์„ ์‚ฌ์šฉํ•˜์—ฌ JSON ํŒŒ์ผ์„ ์‰ฝ๊ฒŒ ์ฝ๊ณ  ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์Œ์€ JSON ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋Š” ์˜ˆ์ œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

import json

# ๋”•์…”๋„ˆ๋ฆฌ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
dict_list = [
    {
        'name': 'emma',  # ํ•™์ƒ ์ด๋ฆ„
        'school': 'Central High School',  # ํ•™๊ต ์ด๋ฆ„
        'grade/class': [3, 1]  # ํ•™๋…„/๋ฐ˜
    },
    {
        'name': 'alice',  # ํ•™์ƒ ์ด๋ฆ„
        'school': 'Seoul Girls Middle School',  # ํ•™๊ต ์ด๋ฆ„
        'grade/class': [2, 2]  # ํ•™๋…„/๋ฐ˜
    }
]

# ๋”•์…”๋„ˆ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜(์ธ์ฝ”๋”ฉ)
json_string = json.dumps(dict_list)

# JSON ํŒŒ์ผ๋กœ ์ €์žฅ
with open('./jsonfile/dictList.json', 'wt') as file:
    file.write(json_string)  # JSON์œผ๋กœ ์ธ์ฝ”๋”ฉํ•œ ๋‚ด์šฉ์„ JSON ํŒŒ์ผ์— ์“ด๋‹ค

print('dictList.json ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.')


์ฃผ์–ด์ง„ ๋”•์…”๋„ˆ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ JSON ํ˜•์‹์˜ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๊ทธ ๋ฌธ์ž์—ด์„ JSON ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค. ๋จผ์ € json.dumps()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋”•์…”๋„ˆ๋ฆฌ ๋ฆฌ์ŠคํŠธ๋ฅผ JSON ํ˜•์‹์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ์ด๋ฅผ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค. ๊ทธ ํ›„์— ์ถœ๋ ฅ๋ฌธ์„ ํ†ตํ•ด ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

# json ํŒŒ์ผ ์ฝ๊ธฐ
import json

with open('./jsonfile/dictList.json', 'rt') as file:
    json_reader = file.read()  # ํŒŒ์ผ ์ „์ฒด ์ฝ์–ด json_reader์— ์ €์žฅ
    dict_list = json.loads(json_reader)  # JSON์„ ํŒŒ์ด์ฌ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ (๋””์ฝ”๋”ฉ)

# ๋ณ€ํ™˜๋œ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ๋”•์…”๋„ˆ๋ฆฌ์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅ
for dic in dict_list:
    print('์ด๋ฆ„: {}'.format(dic['name']))
    print('ํ•™๊ต: {}'.format(dic['school']))
    print('ํ•™๋…„/๋ฐ˜: {}'.format(dic['grade/class'][0]), end=' / ')
    print(dic['grade/class'][1])
    print('----------------')


์ด๋ฒˆ์—๋Š” ์ฃผ์–ด์ง„ ๊ฒฝ๋กœ์— ์žˆ๋Š” JSON ํŒŒ์ผ์„ ์ฝ์–ด์„œ ํŒŒ์ด์ฌ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๋ณ€ํ™˜๋œ ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ ํ•ญ๋ชฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


3. ์‘์šฉ ์˜ˆ์ œ๐ŸŽ

์•„๋ž˜๋Š” ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›์•„์„œ ํ•ด๋‹น ํŒŒ์ผ์„ ๋ณต์‚ฌํ•˜๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ์ž…๋ ฅ๋œ ํŒŒ์ผ์ด 'txt' ํ™•์žฅ์ž๊ฐ€ ์•„๋‹ ๊ฒฝ์šฐ ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , 'txt' ํ™•์žฅ์ž๋ฅผ ๊ฐ€์ง„ ํŒŒ์ผ์ผ ๊ฒฝ์šฐ ํ•ด๋‹น ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ ๋ณต์‚ฌ๋ณธ ํŒŒ์ผ์— ์”๋‹ˆ๋‹ค. ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณต์‚ฌ๋ณธ ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜์—ˆ์Œ์„ ์‚ฌ์šฉ์ž์—๊ฒŒ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

while True:
    filename = input('๋ณต์‚ฌํ•  ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š” >>> ')  # ์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ๋ณต์‚ฌํ•  ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ ๋ฐ›๋Š”๋‹ค
    extname = filename[filename.rfind('.') + 1:]  # ํŒŒ์ผ๋ช…์—์„œ ํ™•์žฅ์ž๋ฅผ ์ถ”์ถœํ•œ๋‹ค

    # ํ™•์žฅ์ž๊ฐ€ 'txt'๊ฐ€ ์•„๋‹ˆ๋ฉด ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๊ณ  ๋‹ค์‹œ ํŒŒ์ผ๋ช…์„ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค
    if extname != 'txt':
        print('๋ณต์‚ฌํ•  ์ˆ˜ ์—†๋Š” ํŒŒ์ผ์ž…๋‹ˆ๋‹ค.')
    else:
        break  # 'txt' ํ™•์žฅ์ž์ธ ๊ฒฝ์šฐ ๋ฃจํ”„๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ํŒŒ์ผ ๋ณต์‚ฌ๋ฅผ ์ง„ํ–‰ํ•œ๋‹ค

# ์›๋ณธ ํŒŒ์ผ์„ ์ฝ์–ด์™€์„œ ๋ณต์‚ฌ๋ณธ ํŒŒ์ผ์— ์“ฐ๋Š” ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•œ๋‹ค
with open(filename, 'rt') as source:  # ์›๋ณธ ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('rt')๋กœ ์—ด์–ด์„œ source ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค
    with open('๋ณต์‚ฌ๋ณธ-' + filename, 'wt') as copy:  # ๋ณต์‚ฌ๋ณธ ํŒŒ์ผ์„ ์“ฐ๊ธฐ ๋ชจ๋“œ('wt')๋กœ ์—ด์–ด์„œ copy ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค
        while True:
            buffer = source.read(1)  # ์›๋ณธ ํŒŒ์ผ์—์„œ 1๋ฐ”์ดํŠธ์”ฉ ์ฝ์–ด buffer์— ์ €์žฅํ•œ๋‹ค


์ด ์ฝ”๋“œ๋Š” ์„œ์šธํŠน๋ณ„์‹œ ๊ฐ•๋‚จ๊ตฌ์— ์„ค์น˜๋œ cctv์˜ ์ด๊ฐœ์ˆ˜๋ฅผ ์„ธ๋Š” ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. CSV ํŒŒ์ผ์„ ์ฝ๊ณ , ๊ฐ ํ–‰์˜ 7๋ฒˆ์งธ ์—ด์— ์žˆ๋Š” ๊ฐ’(์„ค์น˜๋œ cctv ๊ฐœ์ˆ˜)์„ ํ•ฉ์‚ฐํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

import csv

# './csvfile/cctv.csv' ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('rt')๋กœ ์—ด๊ณ  ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ csvfile์— ํ• ๋‹นํ•œ๋‹ค
with open('./csvfile/cctv.csv', 'rt') as csvfile:
    # csv.reader๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ csvfile์„ ์ฝ๋Š”๋‹ค. delimiter๋Š” ์‰ผํ‘œ(','), quotechar๋Š” ํฐ ๋”ฐ์˜ดํ‘œ('"')๋กœ ์„ค์ •ํ•œ๋‹ค.
    buffer = csv.reader(csvfile, delimiter=',', quotechar='"')
    totalcctv = 0  # cctv ๊ฐœ์ˆ˜๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜๋ฅผ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค

    # enumerate() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒ„ํผ์˜ ๊ฐ ํ–‰์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค
    for i, line in enumerate(buffer):
        if i != 0:  # ์ฒซ ๋ฒˆ์งธ ์ค„์€ ์ œ๋ชฉ์ด๋ฏ€๋กœ ์ œ์™ธํ•œ๋‹ค
            totalcctv += int(line[6])  # ๊ฐ ํ–‰์˜ 7๋ฒˆ์งธ ์—ด(์ธ๋ฑ์Šค 6)์— ์žˆ๋Š” ๊ฐ’(์„ค์น˜๋œ cctv ๊ฐœ์ˆ˜)์„ totalcctv์— ๋”ํ•œ๋‹ค

# ์„ค์น˜๋œ cctv์˜ ์ด ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค
print('์„œ์šธํŠน๋ณ„์‹œ ๊ฐ•๋‚จ๊ตฌ์— ์„ค์น˜๋œ cctv๋Š” ์ด {}๊ฐœ์ž…๋‹ˆ๋‹ค.'.format(totalcctv))


์ด๋ฒˆ์—๋Š” 'cctv.json' ํŒŒ์ผ์—์„œ ์ฝ์€ ๋ฐ์ดํ„ฐ๋ฅผ ํŒŒ์ด์ฌ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•˜๊ณ , ๊ฐ ์žฅ์†Œ(place)์˜ '์„ค์น˜๋ชฉ์ ๊ตฌ๋ถ„'์„ ์„ธํŠธ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ์ค‘๋ณต๋˜์ง€ ์•Š๋Š” ๋ชฉ์  ๊ตฌ๋ถ„์ด ์„ธํŠธ์— ๋‹ด๊ฒจ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

import json

# './jsonfile/cctv.json' ํŒŒ์ผ์„ ์ฝ๊ธฐ ๋ชจ๋“œ('rt')๋กœ ์—ด๊ณ  ํŒŒ์ผ ๊ฐ์ฒด๋ฅผ jsonfile์— ํ• ๋‹นํ•œ๋‹ค
with open('./jsonfile/cctv.json', 'rt', encoding='utf-8') as jsonfile:
    buffer = jsonfile.read()  # ํŒŒ์ผ ๋‚ด์šฉ์„ ๋ฌธ์ž์—ด๋กœ ์ฝ์–ด์˜จ๋‹ค
    cctv_list = json.loads(buffer)  # JSON ๋ฌธ์ž์—ด์„ ํŒŒ์ด์ฌ ๊ฐ์ฒด๋กœ ๋ณ€ํ™˜ํ•œ๋‹ค
    cctv_purpose = set()  # ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์„ ๋นˆ ์„ธํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค (์ค‘๋ณต ์ œ์™ธํ•˜๊ธฐ ์œ„ํ•ด)

    # cctv_list์˜ ๊ฐ ์žฅ์†Œ(place)์— ๋Œ€ํ•ด ๋ฐ˜๋ณตํ•œ๋‹ค
    for place in cctv_list:
        cctv_purpose.add(place['์„ค์น˜๋ชฉ์ ๊ตฌ๋ถ„'])  # ๊ฐ ์žฅ์†Œ์˜ '์„ค์น˜๋ชฉ์ ๊ตฌ๋ถ„'์„ ์„ธํŠธ์— ์ถ”๊ฐ€ํ•œ๋‹ค (์ค‘๋ณต๋œ ๊ฐ’์€ ์ถ”๊ฐ€๋˜์ง€ ์•Š๋Š”๋‹ค)

print(cctv_purpose)  # ์„ค์น˜๋œ cctv์˜ ๋ชฉ์  ๊ตฌ๋ถ„์„ ์ถœ๋ ฅํ•œ๋‹ค

4. ํ•ต์‹ฌ ๋‚ด์šฉ๐Ÿ‘€

csv json python
csv python
json python

728x90
๋ฐ˜์‘ํ˜•