0

I've been racking my brain trying to figure out why I can't upload a file to google drive through the API service. I've pin pointed the issue to be related to the special character in Pokémon. If I change that to a regular e, there is no issue uploading the file.

I have already found tried the following method which works but I lose the é altogether.

.encode('ascii','ignore').decode('ascii')

Is there a way to change the encoding that the API uses to upload a file? The error I constantly get is

UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 3: ordinal not in range(128)

My sample code is listed below

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)

Lastly, I have done this manually using the actual google Drive with a file simply containing Pokémon and I never lost the é so I feel like this must be possible.

Mike Lanza
  • 67
  • 1
  • 9

1 Answers1

0

At .encode('ascii','ignore'), é is ignore. So how about uploading the data using BytesIO? Could you please try to modify as follows?

From :

test = 'Pokémon, test, hello\nmark,john,kevin'.encode('ascii','ignore').decode('ascii')
fh = io.StringIO(test)

To :

test = 'Pokémon, test, hello\nmark, john, kevin'.encode('utf-8')
fh = io.BytesIO(test)

Reference :

In my environment, this works. But if this was not the solution of your situation, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Yes, this worked! I originally tried BytesIO but this failed. Long story short, I'm actually working with a Pandas DataFrame, converting to a .csv, and then uploading that as a Google Sheet. This was failing within BytesIO because the pandas .to_csv() results in a string. After reading your answer, I realized I can achieve a solution by adding the .encode('utf-8') to the end and upload this way. Thanks again! – Mike Lanza May 29 '18 at 14:27
  • @Mike Lanza I'm glad your issue was solved. Thank you, too! – Tanaike May 29 '18 at 22:07