Reputation: 27
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
Upvotes: 2
Views: 19128
Reputation: 95
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.
Upvotes: -2
Reputation: 1
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
Upvotes: -2
Reputation: 143206
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
Upvotes: 6