gpt4 book ai didi

python - 在 zipfile 中从 URL 读取 shapefile 时,错误为初始值不是 str

转载 作者:行者123 更新时间:2023-12-01 08:13:10 24 4
gpt4 key购买 nike

使用 Python 3.5,以下示例从 URL 读取 shapefile zipfile 并更新为 P3.5。代码如下。我查看了其他案例并尝试附加 .decode('utf-8') 但这没有帮助。

    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")

错误代码是:类型错误:initial_value 必须是 str 或 None,而不是 bytes

如果我输入 .decode('utf-8'), ala:

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

我收到以下消息:UnicodeDecodeError:“utf-8”编解码器无法解码位置 57 中的字节 0xa4:无效的起始字节

将decode() 放在io.StringIO 调用的外部对原始问题没有影响。我迷路了。我可以尝试什么或者我需要学习什么?谢谢。

此外,我正在尝试实现一个不会在磁盘、临时文件或其他文件上创建文件的过程,因为 Jupyter 和 Macos 之间的关系禁止访问磁盘上的文件。这是另一个尚未有人能够解决的问题。

最佳答案

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")

改用 BytesIO。

StringIO 接受一个字符串,而您传递给它的数据是字节。

关于python - 在 zipfile 中从 URL 读取 shapefile 时,错误为初始值不是 str,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55110509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com