python - Python 3.9 – 使用 pandas 读取 google sheet 文件会导致回溯错误
问题描述
我正在尝试使用熊猫从公开可用的谷歌电子表格中读取数据。
我一直在关注本教程,但在运行我的代码之后:
#!/Library/Frameworks/Python.framework/Versions/3.9/bin/python3
import pandas as pd
# Import data from Google Sheets
sheet_id = "1ierEhD6gcq51HAm433knjnVwey4ZE5DCnu1bW7PRG3E"
df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?forat=csv")
print(df)
我收到这些回溯错误:
回溯(最后一次调用):文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 1342 行,在 do_open h.request(req.get_method() , req.selector, req.data, headers, File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py", line 1255, in request self._send_request(method, url, body, headers, encode_chunked) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1301 行,在 _send_request self.endheaders(body, encode_chunked=encode_chunked )文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第1250行,在endheaders self._send_output(message_body,encode_chunked=encode_chunked)文件“/Library/Frameworks /Python.framework/Versions/3.9/lib/python3.9/http/client。py”,第 1010 行,在 _send_output self.send(msg) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 950 行,在发送 self.connect () 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/http/client.py”,第 1424 行,在 connect self.sock = self._context.wrap_socket(self.sock, File “/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”,第 500 行,在 wrap_socket 中返回 self.sslsocket_class._create(文件“/Library/Frameworks/Python.framework/Versions/ 3.9/lib/python3.9/ssl.py”,第 1040 行,在 _create self.do_handshake() 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ssl.py”,行1309,在 do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL:CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1123)
在处理上述异常的过程中,又出现了一个异常:
回溯(最后一次调用):df = pd.read_csv(文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py”,第 605 行,在 read_csv 返回 _read(filepath_or_buffer, kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py”,第 457 行,在 _read 解析器中= TextFileReader(filepath_or_buffer, **kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py”,第 814 行,在init self._engine = self._make_engine(self.engine) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py”,第 1045 行,在_make_engine return mapping[engine](self.f, **self.options) # type: ignore[call-arg] File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages /pandas/io/parsers.py”,第 1862 行,在init中 self._open_handles(src, kwds) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py”,第 1357 行,在 _open_handles self.handles = get_handle(文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/common.py”,第 558 行,在 get_handle ioargs = _get_filepath_or_buffer(文件“/Library /Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/common.py”,第 288 行,在 _get_filepath_or_buffer req = urlopen(filepath_or_buffer) 文件“/Library/Frameworks/Python. framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/common.py”,第 194 行,在 urlopen 返回 urllib.request.urlopen(*args, **kwargs) 文件“/Library/Frameworks /Python.framework/Versions/3.9/lib/python3.9/urllib/request.py",第 214 行,在 urlopen 返回 opener.open(url, data, timeout) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 517 行,在开放响应中= self._open(req, data) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 534 行,在 _open 结果 = self._call_chain(self. handle_open,协议,协议 + 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 494 行,在 _call_chain 结果 = func(*args) 文件“/Library /Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py",第 1385 行,在 https_open 返回 self.do_open(http.client.HTTPSConnection, req, File"/Library/Frameworks/Python .framework/Versions/3.9/lib/python3.9/urllib/request.py”,第 1345 行,在 do_open 中引发 URLError(err) urllib.error.URLError: <urlopen 错误 [SSL: CERTIFICATE_VERIFY_FAILED] 证书验证失败:无法获取本地颁发者证书 (_ssl.c:1123)>
有人可以帮我吗?
编辑: @Code-Apprentice 的回答部分解决了我的问题,现在我只收到这些错误:
Traceback (most recent call last):
df = pd.read_csv(
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 605, in read_csv
return _read(filepath_or_buffer, kwds)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 457, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 814, in __init__
self._engine = self._make_engine(self.engine)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 1045, in _make_engine
return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/pandas/io/parsers.py", line 1893, in __init__
self._reader = parsers.TextReader(self.handles.handle, **kwds)
File "pandas/_libs/parsers.pyx", line 518, in pandas._libs.parsers.TextReader.__cinit__
File "pandas/_libs/parsers.pyx", line 620, in pandas._libs.parsers.TextReader._get_header
File "pandas/_libs/parsers.pyx", line 814, in pandas._libs.parsers.TextReader._tokenize_rows
File "pandas/_libs/parsers.pyx", line 1943, in pandas._libs.parsers.raise_parser_error
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 54: invalid continuation byte
解决方案
您在参数中有错字,forat
而不是format
. 虽然我不确定这是否是问题所在,但这可能并不好。
推荐阅读
- java - 未指定大小的 Android ThumbnailUtils.createVideoThumbnail 在 API 级别 29 中已弃用
- php - 使用 Curl 调用 API
- loopback4 - 为什么默认环回 4,当我创建环回 4 默认 REST CRUD 控制器时不支持多次插入?
- python - 有没有办法解决 Django 项目中的操作系统错误
- javascript - Rxjs 使用 first() 和 timer()
- laravel - 未找到错误列,但我没有声明该列?
- apache-spark - 从 s3 传递自定义 log4j.properties 文件
- linux - 如何在linux中使用join命令将两个不同长度和不同列的文本文件与标题匹配
- apache-spark - Spark Streaming 的实时推理
- node.js - 在一定时间后使 Mongodb 文档处于非活动状态