Home > Software design >  I am trying to dowload a csv in python but all i get is weird symbols
I am trying to dowload a csv in python but all i get is weird symbols

Time:12-07

I am trying to download this csv in this link when i download with the browser it's ok but the code I can't https://www.policyuncertainty.com/media/Global_Policy_Uncertainty_Data.csv I have tried using pandas with utf-8 and latin-1:

If I do it without encoding I get this error : 'utf-8' codec can't decode byte 0xf5 in position 5: invalid start byte

data = pd.read_csv('https://www.policyuncertainty.com/media/Global_Policy_Uncertainty_Data.csv',encoding='iso-8859-1')

I also tried with this code

url = "https://www.policyuncertainty.com/media/Global_Policy_Uncertainty_Data.csv"
response = requests.get(url)        

with open(ruta "/Global_Policy_Uncertainty_Data.csv", "w") as f:
    writer = csv.writer(f)
    for line in response.iter_lines():
        print(line)
        writer.writerow(line.decode("ISO-8859-1").split("",""),escapechar="/")

but I always get this weird symbols in the file probably because I don't have the right encoding, but when I use postman it gives me a normal data

CodePudding user response:

Changing https to http in the url solved the issue for me:

data = pd.read_csv('http://www.policyuncertainty.com/media/Global_Policy_Uncertainty_Data.csv')

The issue could be from due to an invalid URL scheme. From pandas read_csv documentation: 'Valid URL schemes include http, ftp, s3, gs, and file.

  • Related