UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to <unde

3,251 views
Skip to first unread message

Goutham Sai Siddardha

unread,
Jun 8, 2015, 7:58:50 AM6/8/15
to pyd...@googlegroups.com
Hi, 
I am trying to import '.csv' file and when i am reading the csv log file and writing to another csv file into a well formatted csv file.
but when i am trying to read the file. i am getting this error on 25744th line.
i am new to python and don't understand how to solve this.
i even searched the line in '.csv' file but i don't find any special character
when i removed this line and tried again, i am getting this error for the next line.
please help me solve this problem.
i am really interested in learning python.

Traceback (most recent call last):
  File "C:\Users\SGADI\workspace\DAB_Trace\my_code\trace_parcer.py", line 19, in <module>
    for row in reader:
  File "C:\Users\SGADI\Desktop\Python-32bit-3.4.3.2\python-3.4.3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to <undefined>



this is my code:

import csv
import re
#import matplotlib
#import matplotlib.pyplot as plt
import datetime
#import pandas
#from dateutil.parser import parse
#def parse_csv_file():
timestamp = datetime.datetime.strptime('00:00:00.000', '%H:%M:%S.%f')
timestamp_list = []
snr_list = []
freq_list = []
rssi_list = []
dab_present_list = []
counter = 0
f =  open("output.txt","w")
with open('test_log_20150325_gps.csv', encoding='cp1252') as csvfile:
    reader = csv.reader(csvfile, delimiter=';') 
    for row in reader:
        #timestamp = datetime.datetime.strptime(row[0], '%M:%S.%f')
        #timestamp.split(" ",1)
        
        timestamp = row[0]
        timestamp_list.append(timestamp)
          

        #timestamp = row[0]
        details = row[-1]
        counter += 1
        print (counter)
        #if(counter > 25000):
        #  break
        #timestamp = datetime.datetime.strptime(row[0], '%M:%S.%f')  
      


        #timestamp_list.append(float(timestamp))

        #search for SNRLevel=\d+
        snr = re.findall('SNRLevel=(\d+)', details)
        if snr == []:
            snr = 0
        else:
            snr = snr[0]
        snr_list.append(int(snr))
                            
        #search for Frequency=09ABC
        freq = re.findall('Frequency=([0-9a-fA-F]+)', details)
        if freq == []:
            freq = 0
        else:
            freq = int(freq[0], 16)
        freq_list.append(int(freq))

        #search for RSSI=\d+
        rssi = re.findall('RSSI=(\d+)', details)
        if rssi == []:
            rssi = 0
        else:
            rssi = rssi[0]
        rssi_list.append(int(rssi))

        #search for DABSignalPresent=\d+
        dab_present = re.findall('DABSignalPresent=(\d+)', details)
        if dab_present== []:
            dab_present = 0
        else:
            dab_present = dab_present[0]
        dab_present_list.append(int(dab_present))

        f.write(str(timestamp) + "\t")
        f.write(str(freq) + "\t")
        f.write(str(snr) + "\t")
        f.write(str(rssi) + "\t")
        f.write(str(dab_present) + "\n")
        print (timestamp, freq, snr, rssi, dab_present)

        #print (index+1)

        #print(timestamp,freq,snr)
        #print (counter)
#print(timestamp_list,freq_list,snr_list,rssi_list)


'''if  snr != []:
           if freq != []:
               timestamp_list.append(timestamp)
               snr_list.append(snr)
               freq_list.append(freq)
f.write(str(timestamp_list) + "\t")
f.write(str(freq_list) + "\t")
f.write(str(snr_list) + "\n")
        
print(timestamp_list,freq_list,snr_list)'''
f.close()
#for i in timestamp:
#  timestamp
#return plt.plot(timestamp , freq)
#print (timestamp)
#timestamp.plot()
                            #search for RSSI'''
    #return [timestamp, snr, freq]
#main
'''mydata = []
mydata= parse_csv_file()
'''
'''time = mydata[0]
snr = mydata[1]
freq = mydata[2]
'''



Paul Hobson

unread,
Jun 8, 2015, 12:02:52 PM6/8/15
to pyd...@googlegroups.com
On Tue, Jun 2, 2015 at 6:07 AM, Goutham Sai Siddardha <sidda...@gmail.com> wrote:
Hi, 
I am trying to import '.csv' file and when i am reading the csv log file and writing to another csv file into a well formatted csv file.
but when i am trying to read the file. i am getting this error on 25744th line.
i am new to python and don't understand how to solve this.
i even searched the line in '.csv' file but i don't find any special character
when i removed this line and tried again, i am getting this error for the next line.
please help me solve this problem.
i am really interested in learning python.

Traceback (most recent call last):
  File "C:\Users\SGADI\workspace\DAB_Trace\my_code\trace_parcer.py", line 19, in <module>
    for row in reader:
  File "C:\Users\SGADI\Desktop\Python-32bit-3.4.3.2\python-3.4.3\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 7240: character maps to <undefined>

It appears you're trying to read a unicode file as ascii. Try passing `encoding='utf-8' ` to you call to pandas.read_csv.

Reply all
Reply to author
Forward
0 new messages