-1
  • I have a function who reads MemoryObject of file. It is reading most of the files but for one file it is raising UnicodeDecodeError.

here is my code

def read(file):
    """

    :param file: File Memory Object (submitted from POST)
    :return: File Iterable object
    """

    file = StringIO(file.read().decode())
    return csv.DictReader(file, delimiter=',')
  • File which raise no issue.
  • File which is rasing issue.

Complete error is as follows : UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd0 in position 0: invalid continuation byte

  • In other question where user have asked questions of similar errors. they are using open() to streamline the file but I already have streamline MemoryObject so can't use open()
user5594493
  • 1,050
  • 3
  • 10
  • 24
  • 1
    Possible duplicate of [Python Throwing "'utf8' codec can't decode byte 0xd0 in position 0" Error](https://stackoverflow.com/questions/19117836/python-throwing-utf8-codec-cant-decode-byte-0xd0-in-position-0-error) – Caramiriel Jan 09 '19 at 08:33
  • @Caramiriel here they are using `open()` to steamline the file whereas I already have `Memoryobject` which so can't use open(). – user5594493 Jan 09 '19 at 08:36
  • Is the error thrown at the line including `.decode()`? – MisterMiyagi Jan 09 '19 at 09:05

1 Answers1

1

Your file is already opened in binary mode: decode is a method of bytes, not str.

For your problem, the encoding and errors parameter of bytes.decode works the same as for open. You can apply the appropriate encoding, or ignore errors:

def read(file, encoding: str = 'utf-8', errors: str = 'strict'):
    """
    :param file: File Memory Object (submitted from POST)
    :return: File Iterable object
    """

    file = StringIO(file.read().decode(encoding=encoding, errors=errors))
    return csv.DictReader(file, delimiter=',')

Note that you either must know the encoding, or suppress errors by ignoring them. You can try different encodings to find one that works, but in the end you must know what your data means.

MisterMiyagi
  • 44,374
  • 10
  • 104
  • 119
  • -**FIrst** I am getting this error for ignore keyword `TypeError: 'ignore' is an invalid keyword argument for this function`. **Secondly** : The nature of my data is same in both file I shared but one file (**working**) is created from `LIbre office` using `Text CSV format` and other one (not working) created from `MIcrosoft office (microsoft excel 97-2003 format)` even if this file is blank it will still not work. – user5594493 Jan 10 '19 at 11:00
  • @user5594493 I have fixed the keyword, as shown in the documentation. Note that you did not share any files, both links are invalid. – MisterMiyagi Jan 10 '19 at 11:33
  • still no success ... I have updated the file links in my original question – user5594493 Jan 10 '19 at 11:56