473,785 Members | 2,489 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Test for a unicode string

Hi,

I have a regular expression test in a script. When a unicode character
get tested in the regex it gives an error:

UnicodeError: ASCII decoding error: ordinal not in range(128)

Question: Is there a way to test a string for unicode chars (ie. test
if a string will throw the error cited above).

Like:
if unicode string:
print 'string's line #'
else:
process the string

How do I do "if unicode string" cited in pseudo-code above?

Thanks, still using Python 2.1

Oct 24 '07 #1
5 2323
2007/10/24, goldtech <go******@world post.com>:
Question: Is there a way to test a string for unicode chars (ie. test
if a string will throw the error cited above).
yes there ist :)
>>isinstance(u" a", basestring)
True
>>isinstance(u" a", unicode)
True
>>isinstance("a ", unicode)
False
>>isinstance("a ", str)
True
>>isinstance(u" a", str)
False
--
http://noneisyours.marcher.name
http://feeds.feedburner.com/NoneIsYours
Oct 24 '07 #2
goldtech wrote:
I have a regular expression test in a script. When a unicode character
get tested in the regex it gives an error:

UnicodeError: ASCII decoding error: ordinal not in range(128)

Question: Is there a way to test a string for unicode chars (ie. test
if a string will throw the error cited above).
How about:

if ord(max(thestri ng)) >= 128:
print 'whatever you want'

-Scott David Daniels
Sc***********@A cm.Org
Oct 25 '07 #3
I have a regular expression test in a script. When a unicode character
get tested in the regex it gives an error:

UnicodeError: ASCII decoding error: ordinal not in range(128)

Question: Is there a way to test a string for unicode chars (ie. test
if a string will throw the error cited above).

Like:
if unicode string:
print 'string's line #'
else:
process the string
Sure:

try:
s.decode("ascii ")
except UnicodeError:
print 'string's line #'
else:
process the string

Notice that you get a *decoding* error, so you are not testing
Unicode characters, but byte strings.

HTH,
Martin
Oct 25 '07 #4
* goldtech (Wed, 24 Oct 2007 12:09:24 -0700)
I have a regular expression test in a script. When a unicode character
get tested in the regex it gives an error:
As Martin pointed out: you are *not* using unicode...
Question: Is there a way to test a string for unicode chars (ie. test
if a string will throw the error cited above).
There is but this is not your problem and if it was it wouldn't be the
right solution.

Read http://www.amk.ca/python/howto/unicode. You should at least have
a minimal understanding of unicode before trying to use unicode.
Thanks, still using Python 2.1
Best way to get yourself into trouble.
Thorsten
Oct 25 '07 #5
snip...
Like:
if unicode string:
print 'string's line #'
else:
process the string
If I use "re.UNICODE " like: m = re.match(r"\w+" , s, re.UNICODE)

then it seems to fix my problem. Trying to read as much as I can on
unicode....

Oct 25 '07 #6

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

Similar topics

3
17624
by: Michael Weir | last post by:
I'm sure this is a very simple thing to do, once you know how to do it, but I am having no fun at all trying to write utf-8 strings to a unicode file. Does anyone have a couple of lines of code that - opens a file appropriately for output - writes to this file Thanks very much. Michael Weir
18
1870
by: alex | last post by:
Hi there, how can I check if a variable is a structure (i.e. a list)? For my special problem the variable is either a character string OR a list of character strings line So how can I test if a variable 'a' is either a single character string or a list? I tried: if a is list:
4
6072
by: webdev | last post by:
lo all, some of the questions i'll ask below have most certainly been discussed already, i just hope someone's kind enough to answer them again to help me out.. so i started a python 2.3 script that grabs some web pages from the web, regex parse the data and stores it localy to xml file for further use.. at first i had no problem using python minidom and everything concerning
10
8107
by: Nikolay Petrov | last post by:
How can I convert DOS cyrillic text to Unicode
18
34147
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found Encoding.Convert, but that needs byte arrays. Thanks, /Ger
5
2500
by: wanghz | last post by:
Hello, everyone. I have a problem when I'm processing unicode strings. Is it possible to get the 8bit-string representation of any unicode string? Suppose I get a unicode string: a = u'\xc8\xce\xcf\xcd\xc6\xeb'; then, by a.encode('latin-1'); I can get the 8bit-string representation of it, that is, the physical
24
9072
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8 characters for euopean item names, ie special charcaters from an ASCII perspective. I get the following error: > SQLiteCur.execute(sql, row)
14
2867
by: ThazKool | last post by:
I want to see if this code works the way it should on a Big-Endian system. Also if anyone has any ideas on how determine this at compile-time so that I use the right decoding or encoding functions, I would greatly appreciate the help. Thanks, Ché #include <iostream>
7
4024
by: 7stud | last post by:
Based on this example and the error: ----- u_str = u"abc\u9999" print u_str UnicodeEncodeError: 'ascii' codec can't encode character u'\u9999' in position 3: ordinal not in range(128) ------
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4058
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3662
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.