0

so I'm trying to get a csv file with requests and save it to my project:

import requests
import pandas as pd
import csv

def get_and_save_countries():
    url = 'https://www.trackcorona.live/api/countries'
    r = requests.get(url)
    data = r.json()
    data = data["data"]
  
    with open("corona/dash_apps/finished_apps/apicountries.csv","w",newline="") as f:  
        title = "location,country_code,latitude,longitude,confirmed,dead,recovered,updated".split(",") 
        cw = csv.DictWriter(f,title,delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        cw.writeheader()
        cw.writerows(data)

I've managed that but when I try this:

get_data.get_and_save_countries()
df = pd.read_csv("corona\\dash_apps\\finished_apps\\apicountries.csv")

I get this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe9 in position 1: invalid continuation byte

And I have no idea why. Any help is welcome. Thanks.

Yopa
  • 61
  • 3
  • 10
  • Does anything [here](https://stackoverflow.com/questions/28947607/ascii-codec-cant-decode-byte-0xe9) help you? – 89f3a1c Sep 16 '20 at 17:38

2 Answers2

0

Try:

 with open("corona/dash_apps/finished_apps/apicountries.csv","w",newline="", encoding ='utf-8') as f:

to explicitly specify the encoding with encoding='utf-8'

Yannick Funk
  • 1,319
  • 10
  • 23
  • Thank you so much. It worked. It was a minor problem but it saved my entire project. – Yopa Sep 16 '20 at 17:59
0

When you write to a file, the default encoding is locale.getpreferredencoding(False). On Windows that is usually not UTF-8 and even on Linux the terminal could be configured other than UTF-8. Pandas is defaulting to utf-8, so specify encoding='utf8' as another parameter to open.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251