3

I am trying to download a csv file with the following code:

import requests

url = 'https://www.bjs.gov/content/pub/sheets/mhtip.zip'

res = requests.get(url, allow_redirects=True)

csv = res.content.decode('utf-8')
print(csv)

but I get this error:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xad in position 10: invalid start byte

After searching google with this error, I tried this answer:

import requests

url = 'https://www.bjs.gov/content/pub/sheets/mhtip.zip'

res = requests.get(url, allow_redirects=True)

csv = res.content.encode('utf-8').strip()
print(csv)

But get:

AttributeError: 'bytes' object has no attribute 'encode'

What am I missing here?

Lord Elrond
  • 13,430
  • 7
  • 40
  • 80

0 Answers0