'On zipfile read of shapefile from URL, fault for initial value not str

Using Python 3.5, following example for reading shapefile zipfile from URL and updating for P3.5. Code is below. I've looked at other cases and attempted to append .decode('utf-8') and that does not help.

    dls = "https://github.com/ItsMeLarry/Coursera_Capstone/raw/master/tl_2010_25009_tract00%202.zip"
print('Downloading shapefile...')
lynntracts = ZipFile(io.StringIO(urllib.request.urlopen(dls).read()))
print("Done")

Error code is: TypeError: initial_value must be str or None, not bytes

If I put in .decode('utf-8'), ala:

lynntracts = ZipFile(io.StringIO(urllib.request.urlopen(dls).read().decode('utf-8')))

I get the following message: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa4 in position 57: invalid start byte

Putting the decode() on the outside of the io.StringIO call has no impact on the original problem. I'm lost. What can I try or what do I need to study? Thanks.

Also, I'm trying to implement a procedure that doesn't create a file on disk, temp or otherwise, because the relationship between Jupyter and Macos prohibits accessing files on disk. That's another problem that no-one has been able to solve yet.



Solution 1:[1]

import urllib.request
import io
from zipfile import ZipFile

dls = "https://github.com/ItsMeLarry/Coursera_Capstone/raw/master/tl_2010_25009_tract00%202.zip"
print('Downloading shapefile...')
lynntracts = ZipFile(io.BytesIO(urllib.request.urlopen(dls).read())) -----> Edited Line
print("Done")

Use BytesIO instead.

StringIO takes in a string while the data that you are passing it is bytes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Olufemi Adesina