파일 형식으로 데이터를 여러번 요청하기 - helloMinji/chatbot_spotify GitHub Wiki

  • 필요한 패키지 불러오기
import csv

image

파일로 데이터 가져오기

파일의 데이터를 불러와서 list에 저장

    artists = []
    with open('artist_list.csv', encoding='utf-8') as f:
        raw = csv.reader(f)
        for row in raw:
            artists.append(row[0])

:bulb: 오류 해결 위해 encoding 추가
('cp949' codec can't decode byte 0xbf in position 2: illegal multibyte sequence)

for loop를 이용하여 검색 (API) :star:

    for a in artists:
        params = {
            "q": a,    # 기존에 아티스트명이 들어갔던 자리
            "type": "artist",
            "limit": "1"
        }
    
       #  r =  ~~~  insert_row(cursor, artist, 'artists')

Error Handling

        try:
            artist_raw = raw['artists']['items'][0]
            ~~~
            insert_row(cursor, artist, 'artists')
        except:
            logging.error('something worng')
            continue

에러가 발생하더라도 계속 진행하기를 바라기 때문에 continue 처리