실습 CSC를 JSON으로 변환

Updated:

# CSV, JSON 모듈을 임포트
import csv
import json

elice_utils = EliceUtils()

def books_to_json(src_file, dst_file):
    # 아래 함수를 완성
    books = []
    with open(src_file) as src:
        reader = csv.reader(src, delimiter = ',')
        # 각 줄 별로 대응되는 book 딕셔너리를 만든다.
        for row in reader:
            # 책 정보를 저장하는 딕셔너리를 생성
            book = {
                "title": row[0],
                "author": row[1],
                "genre": row[2],
                "pages": int(row[3]),
                "publisher": row[4]
            }
            books.append(book)

    with open(dst_file, 'w') as dst:
        json_string = json.dumps(books)
        dst.write(json_string)

src_file = 'books.csv'
dst_file = 'books.json'
books_to_json(src_file, dst_file)

Categories:

Updated:

Leave a comment