728x90
반응형
json 파일 예시
[
{"title": "Oh Boy", "songId": "30179107", "artist": "AOA", "img": "aaa.jpg"},
{"title": "With ELVIS", "songId": "30179108", "artist": "AOA", "img": "bbb.jpg"},
{"title": "Good Luck", "songId": "8181755", "artist": "AOA", "img": "ccc.jpg"},
...
]
코드
import json
import csv
# music.json 파일을 읽어서 melon.csv 파일에 저장
with open('music.json', 'r', encoding = 'utf-8') as input_file, open('melon.csv', 'w', newline = '') as output_file :
data = json.load(input_file)
'''
data[0] 은 json 파일의 한 줄을 보관 {"title:"Super Duper", "songId": ...}
data[0]['컬럼명'] 은 첫 번째 줄의 해당 컬럼 element 보관
'''
f = csv.writer(output_file)
# csv 파일에 header 추가
f.writerow(["title", "songId", "artist", "img"])
# 노래 제목에 아래 문구가 포함 되어있을 경우 csv 저장하지 않음
matches = ['inst.', 'Inst.']
# write each row of a json file
for datum in data:
# exclude instrument versions
if any(x in datum["title"] for x in matches):
continue
f.writerow([datum["title"], datum["songId"], datum["artist"], datum["img"]])
출처
728x90
반응형