0

Hi I have error when importing my actual CSV file on google sheet, but when I use one of my sample csv file with simple data no error encountered.

Error

Traceback (most recent call last):
  File "LocalToGsheet.py", line 54, in <module>
    sheet_id=gsheet_name(worksheet_name)
  File "LocalToGsheet.py", line 26, in export_csv
    csvContents = csv_file.read()
  File "/usr/lib64/python3.6/codecs.py", line 321, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf1 in position 56025: invalid continuation byte

Upload script

with open(credentials, 'rb') as token:
    credentials = ServiceAccountCredentials.from_json_keyfile_name(credentials)

API = build('sheets', 'v4', credentials=credentials)

export_csv(
    csv=csv,
    sheet_id=gsheet_name(worksheet_name)
)
rodskies
  • 119
  • 1
  • 11
  • As a hint, when trying to figure out what part of the code to show: the traceback is ordered with the `most recent call last`, as it says. Therefore, start from the end, and work backwards until you find something that's in your code. In this case, that's `csvContents = csv_file.read()`. That said, we don't really need context here; the problem here is that you ask the standard library `csv` module to read the file, and don't tell it the text encoding. It assumes `utf-8`; this assumption is incorrect for your file. You need to figure out which encoding it does use. – Karl Knechtel May 17 '22 at 22:53
  • If you're still confused - in particular, if you don't understand what I mean by `encoding` - then you **must** study how text works in modern computer programs. Files on your hard drive - no matter what the format - store **bytes, not text**. Text is an *abstraction*, and it can *only* be *interpreted* from raw data using an encoding, even if it's a trivial one. When you open a file "in text mode" - whether directly via `open()`, or indirectly - that is your chance to specify an encoding. See https://nedbatchelder.com/text/unipain.html for details. – Karl Knechtel May 17 '22 at 22:55

0 Answers0