我一直绞尽脑汁想弄清楚为什么我无法通过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云端硬盘手动完成此操作,文件只包含神奇宝贝,我从未丢失过é所以我觉得这一定是可能的。
答案 0 :(得分:0)
在.encode('ascii','ignore')
,é
被忽略。那么如何使用BytesIO上传数据呢?您可以尝试修改如下吗?
test = 'Pokémon, test, hello\nmark,john,kevin'.encode('ascii','ignore').decode('ascii')
fh = io.StringIO(test)
test = 'Pokémon, test, hello\nmark, john, kevin'.encode('utf-8')
fh = io.BytesIO(test)
在我的环境中,这是有效的。但如果这不是你情况的解决方案,我很抱歉。