使用 Python 和正确的字符集从 Google 下载 sheet

Download a sheet from Google with Python and the correct charset

我使用这个 Python 脚本(感谢 Tanaike)在 Google Spreadsheet 中下载特定的 sheet 作为 CSV 数据:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
import requests

# Script for authorization of pydrive.
gauth = GoogleAuth()
gauth.LocalWebserverAuth()

# Download the specific sheet in Google Spreadsheet as a CSV data.
spreadsheetId = '###' # Please set the Spreadsheet ID.
sheetId = '###' # Please set the sheet ID. (GID)
url = 'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/gviz/tq?tqx=out:csv&gid=' + sheetId
headers = {'Authorization': 'Bearer ' + gauth.credentials.access_token}
res = requests.get(url, headers=headers)
with open('file.csv', 'wb') as f:
    f.write(res.content)

脚本工作正常,但在 CSV 输出中我遇到了字符集问题,我看不到重音字符。

我该如何解决这个问题?

提前致谢

根据您的问题和回复评论,当我将 àèìòù 的文本添加到电子表格和您问题中的 运行 脚本时,创建的文件可以看作是正确输入的文本。所以,很遗憾,我无法复制您的情况。

并且,根据您之前的回复,

Thank you again Tanaike. I only insert the string and launch the script... Nothing else, I can't understand. Even if use the link via browser "https://docs.google.com/spreadsheets/d/[fileid]/export?format=csv&gid=[sheetid]" I download the file without charset problems, but I don't how I can use this link in the script.

当您想将端点从'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/gviz/tq?tqx=out:csv&gid=' + sheetId更改为https://docs.google.com/spreadsheets/d/[fileid]/export?format=csv&gid=[sheetid]时,请按如下方式修改脚本。

发件人:

url = 'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/gviz/tq?tqx=out:csv&gid=' + sheetId

收件人:

url = 'https://docs.google.com/spreadsheets/d/' + spreadsheetId + '/export?format=csv&gid=' + sheetId

注:

  • 在我的环境中,当我测试两个端点时,我可以确认可以看到包含 àèìòù 的正确输入文本。