f



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

For 'mimetypes' in the code given below, python is giving the following error. Kindly help.

>>> import os
>>> matches = []
>>> for (dirname, dirshere, fileshere) in os.walk(r'C:\Python34'):
	for filename in fileshere:
		if filename.endswith('.py'):
			pathname = os.path.join(dirname, filename)
			if 'mimetypes' in open(pathname).read():
				matches.append(pathname)

				
Traceback (most recent call last):
  File "<pyshell#165>", line 5, in <module>
    if 'mimetypes' in open(pathname).read():
  File "C:\Python34\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 0x81 in position 308: character maps to <undefined>
0
satishmlmlml
11/14/2014 11:57:24 AM
comp.lang.python 77058 articles. 5 followers. Post Follow

1 Replies
2267 Views

Similar Articles

[PageSpeed] 32

On Fri, Nov 14, 2014 at 6:57 AM,  <satishmlmlml@gmail.com> wrote:
> For 'mimetypes' in the code given below, python is giving the following error. Kindly help.
>
>>>> import os
>>>> matches = []
>>>> for (dirname, dirshere, fileshere) in os.walk(r'C:\Python34'):
>         for filename in fileshere:
>                 if filename.endswith('.py'):
>                         pathname = os.path.join(dirname, filename)
>                         if 'mimetypes' in open(pathname).read():
>                                 matches.append(pathname)
>
>
> Traceback (most recent call last):
>   File "<pyshell#165>", line 5, in <module>
>     if 'mimetypes' in open(pathname).read():
>   File "C:\Python34\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 0x81 in position 308: character maps to <undefined>


You should understand encodings.  There is lots of literature on the
web about unicode and python.  In your case, the fact that you are
reading a file called cp1252.py and your code is raising a
UnicodeDecodeError should be a clue
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com
0
Joel
11/14/2014 12:30:01 PM
Reply:

Similar Artilces:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10442: character maps to <undefined>
Im reading a file. But there seems to be some encoding error. >>> f =3D open(filename) >>> data =3D f.read() Traceback (most recent call last): File "<pyshell#2>", line 1, in <module> data =3D f.read() File "C:\Python30\lib\io.py", line 1724, in read decoder.decode(self.buffer.read(), final=3DTrue)) File "C:\Python30\lib\io.py", line 1295, in decode output =3D self.decoder.decode(input, final=3Dfinal) File "C:\Python30\lib\encodings\cp1252.py", line 23, in decode return codecs.charmap_decode(input,se...

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte
I just started to have this error without changing nothing in my index.html(template) and metrites.py(which ipen the template) [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] Original exception was: [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] Traceback (most recent call last): [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] File "/home/nikos/public_html/cgi-bin/metrites.py", line 19, in <module> [Thu Jul 04 11:35:14 2013] [error] [client 108.162.229.97] host = socket.gethostbyaddr( os.environ['REMOTE_ADDR'] )[0...

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
the following code originally from http://zetcode.com/databases/mysqlpythontutorial/ within the "Writing images" part . import MySQLdb as mdb import sys try: fin = open("Chrome_Logo.svg.png",'rb') img = fin.read() fin.close() except IOError as e: print ("Error %d: %s" % (e.args[0],e.args[1])) sys.exit(1) try: conn = mdb.connect(host='localhost',user='testuser', passwd='test623', db='testdb') cursor = conn.cursor() cursor.execute("INSERT INTO Images SET ...

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte #2
Hello although my code is correct just today i saw this in the error_log [Sun Sep 29 07:44:43 2013] [error] [client 173.245.49.126] Traceback (most recent call last): [Sun Sep 29 07:44:43 2013] [error] [client 173.245.49.126] File "/home/nikos/public_html/cgi-bin/metrites.py", line 30, in <module> [Sun Sep 29 07:44:43 2013] [error] [client 173.245.49.126] host = socket.gethostbyaddr( ipval ) [0] [Sun Sep 29 07:44:43 2013] [error] [client 173.245.49.126] UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 0: invalid start byte ...

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 10: ordinal not in range(128)
I have no idea what is causing this error, or how to fix it. The full error is: Traceback (most recent call last): File "D:\ScriptRuntime\PS\Automation\Handlers\SCMTestToolResourceToolsBAT.py", line 60, in Run PS.Automation.Utility.System.AppendSystemPath(args["PATH"], context) File "D:\ScriptRuntime\PS\Automation\Utility\System.py", line 55, in AppendSys temPath AppendPathVariable("PATH", appendtext, context) File "D:\ScriptRuntime\PS\Automation\Utility\System.py", line 37, in AppendPat hVariable if(ap == pp): UnicodeDecod...

RE: UnicodeDecodeError: 'ascii' codec can't decode byte 0xff in position 0: ordinal not in range(128)
This is a multipart message in MIME format. --Boundary_(ID_oK5MBHtGHXoVbM6v7o3uQQ) Content-type: text/plain; charset=utf-8 Content-transfer-encoding: quoted-printable Hi Terry, Thanks for your comment. =20 >To better help us help you, what exact version of Python? I use Python 2.7.5 =20 >Please post plain text without html. Sorry, I will do it. =20 >Please post programs single spaced with just occasional blank lines. =20 File my_program.py =20 import wx.aui import paramiko import telnetlib from LinuxHostPage import * from Wind...

[pysqlite 2.0.2] UnicodeDecodeError: 'utf8' codec can't decode bytes in position 44-45: invalid data
A fetchall() call causes the above error. There must be some non-utf8 chars in the table in question and - indeed - there very likely are: The table contains pathnames of files on the harddisk of a German "speaking" PC. What I have already done to try to overcome this is: - Inserted "# coding: iso-8859-1" (w/o the quotes) in (hopefully) every file of my project - All strings I could find prefixed with "u" (w/o the quotes) - Changed plain calls to execute(q) into execute(unicode(q)) Alas, I am not sure if this all makes really sense, and, obviously it doesn't...

UnicodeDecodeError: 'ascii' codec can't decode byte
Hello It seems like I have Unicode data in a CSV file but Python is using a different code page, so isn't happy when I'm trying to read and put this data into an SQLite database with APSW: ======== sql = "INSERT INTO mytable (col1,col2) VALUES (?,?)" cursor.executemany(sql, records("test.tsv")) """ UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 18: ordinal not in range(128) """ ======== What should I do so Python doesn't raise this error? Should I convert data in the CVS file, or is there some f...

Long way around UnicodeDecodeError, or 'ascii' codec can't decode byte
Hello, I'm working on an unicode-aware application. I like to use "print" to debug programs, but in this case it was nightmare. The most popular result of "print" was: UnicodeDecodeError: 'ascii' codec can't decode byte 0xXX in position 0: ordinal not in range(128) I spent two hours fixing it, and I hope it's done. The solution is one of the ugliest hack I ever written, but it solves the pain. The full story and the code is in my blog: http://uucode.com/blog/2007/03/23/shut-up-you-dummy-7-bit-python/ -- Oleg Parashchenko olpa@ http://uucode.com/ ht...

'ascii' codec can't encode character u'\xe4' in position 4: ordinal not in range(128)
Hello friends, I am using pyWPS + GRASS to generate the maps for the given request XML. As my requestxml contains scandinavian letters , i got the following error, " 'ascii' codec can't encode character u'\xe4' in position 4: ordinal not in range(128) " The Request xml also contains encoding specification like <?xml version="1.0" encoding="UTF-8" standalone="yes"?> Then i traced the pywps code and added the following line, inputXml = inputXml.encode("utf-8") But it gives ...

[2.5.1] "UnicodeDecodeError: 'ascii' codec can't decode byte"?
Hello I'm getting this error while downloading and parsing web pages: ===== title = m.group(1) UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 48: ordinal not in range(128) ===== From what I understand, it's because some strings are Unicode, and hence contain characters that are illegal in ASCII. Does someone know how to solve this error? Thank you. Gilles Ganault wrote: > I'm getting this error while downloading and parsing web pages: > > ===== > title = m.group(1) > UnicodeDecodeError: 'ascii' codec can't...

UnicodeDecodeError: 'ascii' codec can't decodee byte 0xff in position 0: ordinal not in range(128)
This is a multipart message in MIME format. --Boundary_(ID_E/4IbDwDEnubOe6Ijgqz/A) Content-type: text/plain; charset=utf-8 Content-transfer-encoding: quoted-printable Hi all, =20 I use Python telnetlib on Windows 7 32 bit. Here is my code: =20 def telnet(self, host, os, username, password): connection =3D telnetlib.Telnet(host) connection.read_until('login: ') connection.write(username + '\r') connection.read_until('assword: ') connection.write(password + '\r') connection...

UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128)
Hi! Can anyone please help me getting rid of this error: UnicodeEncodeError: 'ascii' codec can't encode character u'\xb7' in position 13: ordinal not in range(128) I am not a python programmer (though intend to start learning this wonderful language), I am just using a python script. After doing some search, I found that 0xb7 is a 'middle dot character' that is not interpreted by the python. Even after inserting text = text.replace('\u00b7', '') in the script, the problem still persists. Can anyone please tell me the easiest way to get rid of th...

UnicodeEncodeError: 'ascii' codec can't encode character u'\ua000' in position 0: ordinal not in range(128)
Hi, I am trying to understand what does encode() do. What are the hex representations of "u" in main.py? Why there is UnicodeEncodeError when main.py is piped to xxd? Why there is no such error when it is not piped? Thanks. ~$ cat main.py #!/usr/bin/env python u =3D unichr(40960) + u'abcd' + unichr(1972) print u ~$ cat main_encode.py #!/usr/bin/env python u =3D unichr(40960) + u'abcd' + unichr(1972) print u.encode('utf-8') $ ./main.py =EA=80=80abcd=DE=B4 ~$ cat main.sh #!/usr/bin/env bash set -v ../main.py | xxd ../main_encode.py |...

Web resources about - UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 308: character maps to <undefined> - comp.lang.python

Resources last updated: 1/26/2016 7:53:52 AM