2

I am parsing a csv file and i am getting the below error

import os
import csv
from collections import defaultdict
demo_data = defaultdict(list)

if os.path.exists("infoed_daily _file.csv"):
    f = open("infoed_daily _file.csv", "rt")
    csv_reader = csv.DictReader(f)
    line_no = 0
    for line in csv_reader:
    line_no +=1
    print(line,line_no)

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 2483: character maps to 
<undefined>

Please advise.

Thanks.. -Prasanna.K

Matthias
  • 12,873
  • 6
  • 42
  • 48
Prasanna K
  • 27
  • 1
  • 4
  • 8
  • maybe file uses encoding different then `utf-8` - ie `latin-1`, `cp1250` - and you may have to use it in `open()`. And you could check in Google what char may have code `0x81` and in which encoding it can be. – furas Jan 06 '21 at 09:36
  • when I run `b'\x81'.decode('Latin1')` or `b'\x81'.decode('Latin2')` or `b'\x81'.decode('iso8859')` or `b'\x81'.decode('iso8859-2')` then it runs without error - so your file can be in some of these encodings (or similar encoding) and you have to use it `open(..., encoding='Latin1')` – furas Jan 06 '21 at 09:41
  • Thanks all. I used `encoding="latin-1"` it worked well. thanks so much – Prasanna K Jan 06 '21 at 13:12
  • Does this answer your question? [UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 55: character maps to ](https://stackoverflow.com/questions/39755301/unicodedecodeerror-charmap-codec-cant-decode-byte-0x81-in-position-55-chara) – drammock Aug 05 '22 at 13:58

3 Answers3

4

Error may means you have file in encoding different then UTF-8 which (probably in most systems) is used as default in open()

When I run

 b'\x81'.decode('Latin1') 
 b'\x81'.decode('Latin2') 
 b'\x81'.decode('iso8859') 
 b'\x81'.decode('iso8859-2') 

then it runs without error - so your file can be in some of these encodings (or similar encoding) and you have to use it

 open(..., encoding='Latin1')

or similar.

List of other encodings: codecs: standard encodings

furas
  • 134,197
  • 12
  • 106
  • 148
0
f=open("myfile1.txt",'r')
print(f.read())

Well, for the above code I got an error as: 'charmap' codec can't decode byte 0x81 in position 637: character maps to so i tried changing the name of the file extension and it worked. Happy Coding Thanks! Vani

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
0

you can use '

with open ('filename.txt','r') as f:
      f.write(content)

The good thing is that it automatically closes the file after work is done.

Agog
  • 69
  • 5