f



SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

The script is very simple (abc.txt exists in ROOTDIR directory):

import os
import shutil

ROOTDIR = 'C:\Users\zoran'

file1 = os.path.join(ROOTDIR, 'abc.txt')
file2 = os.path.join(ROOTDIR, 'def.txt')

shutil.move(file1, file2)


But it returns the following error:


C:\Python34\python.exe C:/Users/bckslash_test.py
  File "C:/Users/bckslash_test.py", line 4
    ROOTDIR = 'C:\Users'
             ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Process finished with exit code 1

As I saw, I could solve the problem by changing line 4 to (small letter "r" before string:
ROOTDIR = r'C:\Users\zoran'

but that is not an option for me because I am using configparser in order to read the ROOTDIR from underlying cfg file.

I need a mechanism to read the path string with single backslashes into a variable, but afterwards to escape every backslash in it. 

How to do that?
0
zljubisicmob
5/8/2015 7:00:55 PM
comp.lang.python 77058 articles. 4 followers. Post Follow

13 Replies
5672 Views

Similar Articles

[PageSpeed] 1

On Fri, May 8, 2015, at 15:00, zljubisicmob@gmail.com wrote:
> As I saw, I could solve the problem by changing line 4 to (small letter
> "r" before string:
> ROOTDIR = r'C:\Users\zoran'
> 
> but that is not an option for me because I am using configparser in order
> to read the ROOTDIR from underlying cfg file.

configparser won't have that problem, since "escaping" is only an issue
for python source code. No escaping for backslashes is necessary in
files read by configparser.

>>> import sys
>>> import configparser
>>> config = configparser.ConfigParser()
>>> config['DEFAULT'] = {'ROOTDIR': r'C:\Users\zoran'}
>>> config.write(sys.stdout)
[DEFAULT]
rootdir = C:\Users\zoran
0
random832
5/8/2015 7:29:48 PM
On 2015-05-08 20:00, zljubisicmob@gmail.com wrote:
> The script is very simple (abc.txt exists in ROOTDIR directory):
>
> import os
> import shutil
>
> ROOTDIR = 'C:\Users\zoran'
>
> file1 = os.path.join(ROOTDIR, 'abc.txt')
> file2 = os.path.join(ROOTDIR, 'def.txt')
>
> shutil.move(file1, file2)
>
>
> But it returns the following error:
>
>
> C:\Python34\python.exe C:/Users/bckslash_test.py
>    File "C:/Users/bckslash_test.py", line 4
>      ROOTDIR = 'C:\Users'
>               ^
> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
>
> Process finished with exit code 1
>
> As I saw, I could solve the problem by changing line 4 to (small letter "r" before string:
> ROOTDIR = r'C:\Users\zoran'
>
> but that is not an option for me because I am using configparser in order to read the ROOTDIR from underlying cfg file.
>
> I need a mechanism to read the path string with single backslashes into a variable, but afterwards to escape every backslash in it.
>
> How to do that?
>
If you're reading the path from a file, it's not a problem. Try it!

0
MRAB
5/8/2015 7:33:25 PM
Thanks for clarifying.
Looks like the error message was wrong.
On windows ntfs I had a file name more than 259 characters which is widows limit.
After cutting file name to 259 characters everything works as it should.
If I cut file name to 260 characters I get the error from subject which is wrong.

Anyway case closed, thank you very much because I was suspecting that something is wrong with configparser.

Best regards.
0
zljubisicmob
5/8/2015 8:39:54 PM
On Sat, May 9, 2015 at 5:00 AM,  <zljubisicmob@gmail.com> wrote:
> But it returns the following error:
>
>
> C:\Python34\python.exe C:/Users/bckslash_test.py
>   File "C:/Users/bckslash_test.py", line 4
>     ROOTDIR = 'C:\Users'
>              ^
> SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

Strong suggestion: Use forward slashes for everything other than what
you show to a human - and maybe even then (some programs have always
printed stuff out that way - zip/unzip, for instance). The backslash
has special meaning in many contexts, and you'll just save yourself so
much trouble...

ROOTDIR = 'C:/Users/zoran'

Problem solved!

ChrisA
0
Chris
5/8/2015 10:54:49 PM
On Sat, 9 May 2015 06:39 am, zljubisicmob@gmail.com wrote:

> Thanks for clarifying.
> Looks like the error message was wrong.

No, the error message was right.

Your problem was that you used backslashes in *Python program code*, rather
than reading it from a text file.

In Python, a string-literal containing \U is an escape sequence which
expects exactly 8 hexadecimal digits to follow:


py> path = '~~~~\U000000a7~~~~'
py> print(path)
~~~~§~~~~

If you don't follow the \U with eight hex digits, you get an error:

py> path = '~~~~\Users~~~~'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in
position 4-6: truncated \UXXXXXXXX escape


This applies only to string literals in code. For data read from files,
backslash \ is just an ordinary character which has no special meaning.


> On windows ntfs I had a file name more than 259 characters which is widows
> limit. After cutting file name to 259 characters everything works as it
> should. If I cut file name to 260 characters I get the error from subject
> which is wrong.

What you describe is impossible. You cannot possibly get a SyntaxError at
compile time because the path is too long. You must have made other changes
at the same time, such as using a raw string r'C: ... \Users\ ...'.



-- 
Steven

0
Steven
5/9/2015 2:53:01 AM
Steven,

please do look at the code bellow:

# C:\Users\zoran\PycharmProjects\mm_align\hrt3.cfg contents
# [Dir]
# ROOTDIR = C:\Users\zoran\hrt


import os
import shutil
import configparser
import requests
import re

Config = configparser.ConfigParser()
Config.optionxform = str # preserve case in ini file
cfg_file = os.path.join('C:\\Users\\zoran\\PycharmProjects\\mm_align\\hrt3.cfg' )
Config.read(cfg_file)



ROOTDIR = Config.get('Dir', 'ROOTDIR')

print(ROOTDIR)

html = requests.get("http://radio.hrt.hr/prvi-program/arhiva/ujutro-prvi-poligraf-politicki-grafikon/118/").text

art_html = re.search('<article id="aod_0">(.+?)</article>', html, re.DOTALL).group(1)
for p_tag in re.finditer(r'<p>(.*?)</p>', art_html, re.DOTALL):
    if '<strong>' not in p_tag.group(1):
        title = p_tag.group(1)

title = title[:232]
title = title.replace(" ", "_").replace("/", "_").replace("!", "_").replace("?", "_")\
                    .replace('"', "_").replace(':', "_").replace(',', "_").replace('&#34;', '')\
                    .replace('\n', '_').replace('&#39', '')

print(title)

src_file = os.path.join(ROOTDIR, 'src_' + title + '.txt')
dst_file = os.path.join(ROOTDIR, 'des_' + title + '.txt')

print(len(src_file), src_file)
print(len(dst_file), dst_file)

with open(src_file, mode='w', encoding='utf-8') as s_file:
    s_file.write('test')


shutil.move(src_file, dst_file)

It works, but if you change title = title[:232] to title = title[:233], you will get "FileNotFoundError: [Errno 2] No such file or directory".
As you can see ROOTDIR contains \U.

Regards.
0
zljubisicmob
5/9/2015 10:31:08 AM
On 05/09/2015 06:31 AM, zljubisicmob@gmail.com wrote:
>
> title = title[:232]
> title = title.replace(" ", "_").replace("/", "_").replace("!", "_").replace("?", "_")\
>                      .replace('"', "_").replace(':', "_").replace(',', "_").replace('&#34;', '')\
>                      .replace('\n', '_').replace('&#39', '')
>
> print(title)
>
> src_file = os.path.join(ROOTDIR, 'src_' + title + '.txt')
> dst_file = os.path.join(ROOTDIR, 'des_' + title + '.txt')
>
> print(len(src_file), src_file)
> print(len(dst_file), dst_file)
>
> with open(src_file, mode='w', encoding='utf-8') as s_file:
>      s_file.write('test')
>
>
> shutil.move(src_file, dst_file)
>
> It works, but if you change title = title[:232] to title = title[:233], you will get "FileNotFoundError: [Errno 2] No such file or directory".
> As you can see ROOTDIR contains \U.

No, we can't see what ROOTDIR is, since you read it from the config 
file.  And you don't show us the results of those prints.  You don't 
even show us the full exception, or even the line it fails on.

I doubt that the problem is in the ROODIR value, but of course nothing 
in your program bothers to check that that directory exists.  I expect 
you either have too many characters total, or the 232th character is a 
strange one.  Or perhaps title has a backslash in it (you took care of 
forward slash).

While we're at it, if you do have an OS limitation on size, your code is 
truncating at the wrong point.  You need to truncate the title based on 
the total size of src_file and dst_file, and since the code cannot know 
the size of ROOTDIR, you need to include that in your figuring.




-- 
DaveA
0
Dave
5/9/2015 12:25:20 PM
On Sat, 9 May 2015 08:31 pm, zljubisicmob@gmail.com wrote:

> It works, but if you change title = title[:232] to title = title[:233],
> you will get "FileNotFoundError: [Errno 2] No such file or directory".


Which is a *completely different* error from 

SyntaxError: 'unicodeescape' codec can't decode bytes in position 2-3:
truncated \UXXXXXXXX escape


> As you can see ROOTDIR contains \U.

How can I possibly see that? Your code reads ROOTDIR from the config file,
which you don't show us.

I agree with you that Windows has limitations on the length of file names,
and that you get an error if you give a file name that cannot be found. The
point is that before you can get that far, you *first* have to fix the
SyntaxError. That's a completely different problem.

You can't fix the \U syntax error by truncating the total file length. But
you can fix that syntax error by changing your code so it reads the ROOTDIR
from a config file instead of a hard-coded string literal -- exactly like
we told you to do!

An essential skill when programming is to read and understand the error
messages. One of the most painful things to use is a programming language
that just says 

"An error occurred"

with no other explanation. Python gives you lots of detail to explain what
went wrong:

SyntaxError means you made an error in the syntax of the code and the
program cannot even run.

FileNotFoundError means that the program did run, it tried to open a file,
but the file doesn't exist.

They're a little bit different, don't you agree?



-- 
Steven

0
Steven
5/9/2015 3:13:56 PM
On Sun, May 10, 2015 at 1:13 AM, Steven D'Aprano
<steve+comp.lang.python@pearwood.info> wrote:
> FileNotFoundError means that the program did run, it tried to open a file,
> but the file doesn't exist.

Normally it does, at least. Sometimes it means that a *directory*
doesn't exist (for instance, you can get this when you try to create a
new file, which otherwise wouldn't make sense), and occasionally,
Windows will give you rather peculiar errors when weird things go
wrong, which may be what's going on here (maximum path length - though
that can be overridden by switching to a UNC-style path).

Steven's point still stands - very different from SyntaxError - but
unfortunately it's not always as simple as the name suggests. Thank
you oh so much, Windows.

ChrisA
0
Chris
5/9/2015 3:22:07 PM
> No, we can't see what ROOTDIR is, since you read it from the config=20
> file.  And you don't show us the results of those prints.  You don't=20
> even show us the full exception, or even the line it fails on.

Sorry I forgot. This is the output of the script:

C:\Python34\python.exe C:/Users/zoran/PycharmProjects/mm_align/bckslash_tes=
t.py
C:\Users\zoran\hrt
Traceback (most recent call last):
  File "C:/Users/zoran/PycharmProjects/mm_align/bckslash_test.py", line 43,=
 in <module>
    with open(src_file, mode=3D'w', encoding=3D'utf-8') as s_file:
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\zoran\\=
hrt\\src_70._godi=C5=A1njica_pobjede_nad_fa=C5=A1izmom_Za=C5=A1to_ve=C4=87i=
na_=C4=8Delnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_=C4=87=
e_se_obljetnica_pobjede_nad_nacisti=C4=8Dkom_Njema=C4=8Dkom_i_njenim_sateli=
tima_obilje=C5=BEiti_u_na=C5=A1oj_zemlji__Ho=C4=87e_li_Josip_Broz_Tito_o.tx=
t'
70._godi=C5=A1njica_pobjede_nad_fa=C5=A1izmom_Za=C5=A1to_ve=C4=87ina_=C4=8D=
elnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_=C4=87e_se_oblj=
etnica_pobjede_nad_nacisti=C4=8Dkom_Njema=C4=8Dkom_i_njenim_satelitima_obil=
je=C5=BEiti_u_na=C5=A1oj_zemlji__Ho=C4=87e_li_Josip_Broz_Tito_o
260 C:\Users\zoran\hrt\src_70._godi=C5=A1njica_pobjede_nad_fa=C5=A1izmom_Za=
=C5=A1to_ve=C4=87ina_=C4=8Delnika_Europske_unije_bojkotira_vojnu_paradu_u_M=
oskvi__Kako_=C4=87e_se_obljetnica_pobjede_nad_nacisti=C4=8Dkom_Njema=C4=8Dk=
om_i_njenim_satelitima_obilje=C5=BEiti_u_na=C5=A1oj_zemlji__Ho=C4=87e_li_Jo=
sip_Broz_Tito_o.txt
260 C:\Users\zoran\hrt\des_70._godi=C5=A1njica_pobjede_nad_fa=C5=A1izmom_Za=
=C5=A1to_ve=C4=87ina_=C4=8Delnika_Europske_unije_bojkotira_vojnu_paradu_u_M=
oskvi__Kako_=C4=87e_se_obljetnica_pobjede_nad_nacisti=C4=8Dkom_Njema=C4=8Dk=
om_i_njenim_satelitima_obilje=C5=BEiti_u_na=C5=A1oj_zemlji__Ho=C4=87e_li_Jo=
sip_Broz_Tito_o.txt

Process finished with exit code 1

Cfg file has the following contents:

C:\Users\zoran\PycharmProjects\mm_align\hrt3.cfg contents
[Dir]
ROOTDIR =3D C:\Users\zoran\hrt=20

> I doubt that the problem is in the ROODIR value, but of course nothing=20
> in your program bothers to check that that directory exists.  I expect=20
> you either have too many characters total, or the 232th character is a=20
> strange one.  Or perhaps title has a backslash in it (you took care of=20
> forward slash).

How to determine that?
=20
> While we're at it, if you do have an OS limitation on size, your code is=
=20
> truncating at the wrong point.  You need to truncate the title based on=
=20
> the total size of src_file and dst_file, and since the code cannot know=
=20
> the size of ROOTDIR, you need to include that in your figuring.

Well, in my program I am defining a file name as category-id-description.mp=
3.
If the file is too long I am cutting description (it wasn't clear from my e=
xample).

Regards.
0
zljubisicmob
5/10/2015 9:10:32 PM
> > It works, but if you change title = title[:232] to title = title[:233],
> > you will get "FileNotFoundError: [Errno 2] No such file or directory".
> 
> 
> Which is a *completely different* error from 
> 
> SyntaxError: 'unicodeescape' codec can't decode bytes in position 2-3:
> truncated \UXXXXXXXX escape

I don't know when the original error disappeared and become this one (confused).

Regards.
0
zljubisicmob
5/10/2015 9:14:48 PM
On 05/10/2015 05:10 PM, zljubisicmob@gmail.com wrote:
>> No, we can't see what ROOTDIR is, since you read it from the config
>> file.  And you don't show us the results of those prints.  You don't
>> even show us the full exception, or even the line it fails on.
>
> Sorry I forgot. This is the output of the script:
>
> C:\Python34\python.exe C:/Users/zoran/PycharmProjects/mm_align/bckslash_test.py
> C:\Users\zoran\hrt
> Traceback (most recent call last):
>    File "C:/Users/zoran/PycharmProjects/mm_align/bckslash_test.py", line 43, in <module>
>      with open(src_file, mode='w', encoding='utf-8') as s_file:
> FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\zoran\\hrt\\src_70._godišnjica_pobjede_nad_fašizmom_Zašto_većina_čelnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_će_se_obljetnica_pobjede_nad_nacističkom_Njemačkom_i_njenim_satelitima_obilježiti_u_našoj_zemlji__Hoće_li_Josip_Broz_Tito_o.txt'
> 70._godišnjica_pobjede_nad_fašizmom_Zašto_većina_čelnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_će_se_obljetnica_pobjede_nad_nacističkom_Njemačkom_i_njenim_satelitima_obilježiti_u_našoj_zemlji__Hoće_li_Josip_Broz_Tito_o
> 260 C:\Users\zoran\hrt\src_70._godišnjica_pobjede_nad_fašizmom_Zašto_većina_čelnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_će_se_obljetnica_pobjede_nad_nacističkom_Njemačkom_i_njenim_satelitima_obilježiti_u_našoj_zemlji__Hoće_li_Josip_Broz_Tito_o.txt
> 260 C:\Users\zoran\hrt\des_70._godišnjica_pobjede_nad_fašizmom_Zašto_većina_čelnika_Europske_unije_bojkotira_vojnu_paradu_u_Moskvi__Kako_će_se_obljetnica_pobjede_nad_nacističkom_Njemačkom_i_njenim_satelitima_obilježiti_u_našoj_zemlji__Hoće_li_Josip_Broz_Tito_o.txt
>
> Process finished with exit code 1
>
> Cfg file has the following contents:
>
> C:\Users\zoran\PycharmProjects\mm_align\hrt3.cfg contents
> [Dir]
> ROOTDIR = C:\Users\zoran\hrt
>
>> I doubt that the problem is in the ROODIR value, but of course nothing
>> in your program bothers to check that that directory exists.  I expect
>> you either have too many characters total, or the 232th character is a
>> strange one.  Or perhaps title has a backslash in it (you took care of
>> forward slash).
>
> How to determine that?

Probably by calling os.path.isdir()

>
>> While we're at it, if you do have an OS limitation on size, your code is
>> truncating at the wrong point.  You need to truncate the title based on
>> the total size of src_file and dst_file, and since the code cannot know
>> the size of ROOTDIR, you need to include that in your figuring.
>
> Well, in my program I am defining a file name as category-id-description.mp3.
> If the file is too long I am cutting description (it wasn't clear from my example).

Since you've got non-ASCII characters in that name, the utf-8 version of 
the name will be longer.  I don't run Windows, but perhaps it's just a 
length problem after all.



-- 
DaveA
0
Dave
5/11/2015 1:33:09 AM
I would say so as well.
Thanks to everyone who helped.

Regards and best wishes.
0
zljubisicmob
5/12/2015 6:57:49 PM
Reply:

Similar Artilces:

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 ...

[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: '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
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...

[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 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...

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()...

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: '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...

Can't figure out where SyntaxError: can not delete variable 'x' referenced in nested scope us coming from in python >=2.6
Say I have module foo.py: def a(x): def b(): x del x If I run foo.py under Python 2.4.4 I get: File "foo.py", line 4 del x SyntaxError: can not delete variable 'x' referenced in nested scope Under Python 2.6 and Python 3.0 I get: SyntaxError: can not delete variable 'x' referenced in nested scope The difference is under Python 2.4 I get a traceback with the lineno and offending line, but I do not get a traceback in Pythons 2.6 and 3.0. The reason why I as...

Wine-20050830: Can't find DLL 'x' needed by 'y', error=2
Obviously, Wine-20050830 is much, much different from earlier versions in that it does not even seem to consider a '.wine/config' file. And, it would seem, there's a problem or misunderstanding about locating DLLs. When I try to start 16-bit "Delphi.exe," I get an error: Could not load 'BIVBX11.DLL' required by 'DELPHI', error=2 A more detailed trace shows: [snip!] trace:module:LdrGetDllHandle 0 0 L"OLE32.DLL" -> 0x7f550000 trace:module:load_dll looking for L"ole32.dll" in L"C:\\delphi\\bin;.;c:\\windows\\syste...

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...

i can't insert 'a''a' in a field with size 3
hi, i run INSERT INTO test (FIELD1) VALUES ('a''a') but DBMS oracle returns this error: ORA-01401: inserted value too large for column (FIELD1 size is 3!) what's the error? Fici wrote: > hi, > i run > INSERT INTO test (FIELD1) VALUES ('a''a') > but DBMS oracle returns this error: > ORA-01401: inserted value too large for column > (FIELD1 size is 3!) > what's the error? SQL> create table test ( 2 field1 VARCHAR2(3)); Table created. SQL> INSERT INTO test 2 (field1) 3 VALUES 4 ('a...

Web resources about - SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape - comp.lang.python

Resources last updated: 2/9/2016 6:56:26 AM