0

I'm trying to download a csv file to local disc and I'm getting the error:

"UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 253967: invalid continuation byte"

What is the best/easiest way to get around this error, and why is this popping up as an error?

import requests
import pandas as pd
import csv

url = 'https://www2.census.gov/programs-surveys/popest/datasets/2010-2019/counties/totals/co-est2019-alldata.csv'

df = pd.read_csv(url)
df = df['SUMLEV', 'REGION', 'DIVISION', 'STATE', 'COUNTY', 'STNAME', 'CTYNAME', 'POPESTIMATE2019']
df.to_csv('C:\localpath.csv', index=False)
  • 1
    Does this solve your issue? -- https://stackoverflow.com/a/61267213/13261176 – Teejay Bruno Aug 04 '21 at 17:23
  • Got it. Yes, it does look like "encoding = 'unicode_escape' " solved my issue. Without adding that in, do you know why this same code works fine with some links, but raises the error for this particular one? Trying to understand what causes the issue. – FormidableData Aug 04 '21 at 17:34

0 Answers0