473,386 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

u'a' in string.letters fails: a Python 2.3 bug?

From the documentation for the string module at:

C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html

[quote]
letters: The concatenation of the strings lowercase and uppercase described
below. The specific value is locale-dependent, and will be updated when
locale.setlocale() is called.
[end quote]

If uch is a unicode character, the operation

uch in string.letters

may (will?) fail in Python 2.3. I've never seen it fail in previous
versions. Examples:

Python 2.3.1 (#47, Sep 23 2003, 23:47:32) [MSC v.1200 32 bit (Intel)] on
win32
[snip]
IDLE 1.0
import string
'\xa6' in string.digits False '\xa6' in string.letters False u'\xa6' in string.letters
Traceback (most recent call last):
File "<pyshell#3>", line 1, in -toplevel-
u'\xa6' in string.letters
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 52:
ordinal not in range(128)u'\xa6' in string.ascii_letters False u'a' in string.letters
Traceback (most recent call last):
File "<pyshell#1>", line 1, in -toplevel-
u'a' in string.letters
UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 52:
ordinal not in range(128)

Questions:

1. Is this a bug, or am I missing something?

2. Is this an issue only with Idle? I think not completely: this kind of
code seems to work for my app on XP, and not for some of my app's users on
Linux.

3. Is replacing string.letters by string.ascii_letters the recommended
workaround?

Edward

P.S.
string.letters

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvw xyz\x83\x8a\x8c\x8e\x9a\x9
c\x9e\x9f\xaa\xb5\xba\xc0\xc1\xc2\xc3\xc4\xc5\xc6\ xc7\xc8\xc9\xca\xcb\xcc\xc
d\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd8\xd9\xda\ xdb\xdc\xdd\xde\xdf\xe0\xe
1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\ xee\xef\xf0\xf1\xf2\xf3\xf
4\xf5\xf6\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff'

EKR
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #1
2 2786
"Edward K. Ream" <ed*******@charter.net> writes:
From the documentation for the string module at:

C:\Python23\Doc\Python-Docs-2.3.1\lib\module-string.html

[quote]
letters: The concatenation of the strings lowercase and uppercase described
below. The specific value is locale-dependent, and will be updated when
locale.setlocale() is called.
[end quote]

If uch is a unicode character, the operation

uch in string.letters

may (will?) fail in Python 2.3. I've never seen it fail in previous
versions.
Must be because you weren't looking <wink>:

Python 2.2.1 (#1, Apr 9 2002, 13:10:27)
[GCC 2.96 20000731 (Red Hat Linux 7.1 2.96-98)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
u'a' in string.letters

Traceback (most recent call last):
File "<stdin>", line 1, in ?
UnicodeError: ASCII decoding error: ordinal not in range(128)

(python from Sean's 2.2.1 RPM on redhat 7.2-ish).
1. Is this a bug, or am I missing something?
What you may be missing is that factors including but not limited to
readline, the way python was invoked, orders of imports, locals
settings and the phase of the moon may have an effect on whether
"ordinals not in range(128)" get into string.letters.

I think the interaction of readline and locale settings got a going
over for 2.3 which *might* explain any differences you're seeing.
2. Is this an issue only with Idle? I think not completely: this
kind of code seems to work for my app on XP, and not for some of my
app's users on Linux.
See above :-)
3. Is replacing string.letters by string.ascii_letters the recommended
workaround?


Err, probably. Depends what you're testing for, I guess. Wouldn't
uch.isalpha() or one of the unicodedata thingies be more appropriate
most of the time?

Cheers,
mwh

PS: on typing control-D into the 2.2.1 session above, I get a
segfault. Now *that's* got to be a bug!

--
surely, somewhere, somehow, in the history of computing, at least
one manual has been written that you could at least remotely
attempt to consider possibly glancing at. -- Adam Rixey
Jul 18 '05 #2
> > 1. Is this a bug, or am I missing something?

What you may be missing is that factors including but not limited to
readline, the way python was invoked, orders of imports, locals
settings and the phase of the moon may have an effect on whether
"ordinals not in range(128)" get into string.letters.


Thanks for this info. I wonder why string.letters remains. Shouldn't it be
deprecated?

I've substituted string.ascii_letters for string.letters as a temporary
expedient, and will consider ch.isalpha() for future work. Thanks again.

Edward
--------------------------------------------------------------------
Edward K. Ream email: ed*******@charter.net
Leo: Literate Editor with Outlines
Leo: http://webpages.charter.net/edreamleo/front.html
--------------------------------------------------------------------
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: google account | last post by:
I know that string.letters and string.digits are all the letters and numbers, is there a string.???? that is a subset of all the ascii characters that are available on a US English Keyboard...
21
by: Andreas Lobinger | last post by:
Aloha, i wanted to ask another problem, but as i started to build an example... How to generate (memory and time)-efficient a string containing random characters? I have never worked with...
11
by: John Velman | last post by:
I've used perl for a lot of 'throw away' scripts; I like Python better in principle, from reading about it, but it was always easier to just use perl rather than learn python. Now I'm writing a...
9
by: Jim Lewis | last post by:
Anyone have experience with string pattern matching? I need a fast way to match variables to strings. Example: string - variables ============ abcaaab - xyz abca - xy eeabcac - vxw x...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
7
by: Johny | last post by:
I have a string of a variable length and I need to split the string in strings of 6 characters . But if the 6th character is not space then I must split the string at possition before the 6th...
1
by: Alexnb | last post by:
Okay, I have a fix for this problem, but it is messy and I think there might be a better way. Heres an example: Lets say I have a string: "My name is alex" and I have another string "My name...
2
by: Edwin.Madari | last post by:
use re module import re template = '^My name is alex' astring = 'My name is alex, and I like pie' if re.match(template, astring): print 'Found it' else: print '%s does not begin with %s' %...
12
by: Alexnb | last post by:
This is similar to my last post, but a little different. Here is what I would like to do. Lets say I have a text file. The contents look like this, only there is A LOT of the same thing. () A...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.