我一直在绞尽脑汁想弄清楚为什么我不能通过 API 服务将文件上传到谷歌驱动器。我已经指出这个问题与神奇宝贝中的特殊角色有关。如果我将其更改为常规 e,则上传文件没有问题。
我已经发现尝试了以下有效的方法,但我完全失去了é。
.encode('ascii','ignore').decode('ascii')
有没有办法更改 API 用于上传文件的编码?我经常得到的错误是
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128)
我的示例代码如下所示
from httplib2 import Http
import io
from apiclient.discovery import build
from apiclient import errors
from apiclient.http import MediaFileUpload, MediaIoBaseDownload, MediaIoBaseUpload
# If modifying these scopes, delete your previously saved credentials
# at ~/.credentials/drive-python-quickstart.json
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'client_secret.json'
APPLICATION_NAME = 'Drive API Python Quickstart'
#performs authorization
authInst = auth(SCOPES,CLIENT_SECRET_FILE,APPLICATION_NAME)
#gets the credentials for use
credentials = authInst.getCredentials()
#sets up the drive service from which functions can be created
drive_service = build('drive', 'v3', http=credentials.authorize(Http()))
test = 'Pokémon, test, hello\nmark,john,kevin'.encode('ascii','ignore').decode('ascii')
fh = io.StringIO(test)
media = MediaIoBaseUpload(fh,
mimetype='text/csv',
resumable = False)
filename = 'csvtest'
file_metadata = {'name': filename, 'mimeType': 'application/vnd.google-apps.spreadsheet'}
file = drive_service.files().create(body=file_metadata,\
media_body=media,\
fields='id').execute()
fileID = file.get('id')
print(fileID)
最后,我使用实际的 google Drive 手动完成了此操作,其中包含一个仅包含 Pokémon 的文件,并且我从未丢失 é,所以我觉得这一定是可能的。