使用Python和正确的字符集从Google下载工作表

使用Python和正确的字符集从Google下载工作表,python,python-requests,character-encoding,google-sheets-api,Python,Python Requests,Character Encoding,Google Sheets Api,我使用这个Python脚本(感谢Tanaike)在Google电子表格中下载了一个特定的表单作为CSV数据: from pydrive.auth import GoogleAuth from pydrive.drive import GoogleDrive import requests # Script for authorization of pydrive. gauth = GoogleAuth() gauth.LocalWebserverAuth() # Download the s

我使用这个Python脚本(感谢Tanaike)在Google电子表格中下载了一个特定的表单作为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输出中我有一个字符集问题,我看不到重音字符

我怎样才能解决这个问题


提前感谢您的提问和回复意见,当我将
的文本添加到电子表格并运行您问题中的脚本时,创建的文件可以被视为正确的输入文本。所以,不幸的是,我无法复制你的情况

从你之前的答复来看


再次感谢你,塔奈克。我只插入字符串并启动脚本。。。没有别的,我不明白。即使通过浏览器使用链接“
https://docs.google.com/spreadsheets/d/[fileid]/export?format=csv&gid=[sheetid]
“我下载的文件没有字符集问题,但我不知道如何在脚本中使用此链接

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

发件人: 致: 注:
  • 在我的环境中,当我测试两个端点时,我可以确认可以看到正确的输入文本,包括
    èèù

不幸的是,我无法复制您的情况。我为此道歉。那么,您能否提供用于复制您的问题的示例电子表格?通过这个,我想确认一下。嗨,塔奈克,谢谢你的回复。我是意大利人,所以如果我在表格中写重音字符,比如说,我在输出csv中看不到它们。相反,如果我通过页面的GUI下载CSV,我可以看到重音字符…感谢您的回复。从您的回复中,当我将
的文本添加到电子表格并运行脚本时,创建的文件可以被视为正确的输入文本。所以,不幸的是,我无法复制你的情况。我为此道歉。我可以问一下复制您的问题的方法吗?再次感谢Tanaike。我只插入字符串并启动脚本。。。没有别的,我不明白。即使通过浏览器“”使用链接,我下载的文件没有字符集问题,但我不知道如何在脚本中使用此链接。谢谢您的回复。从您的回复中,我提出了一个修改点,将端点更改为
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