Re: Test for a unicode string

2007-10-25 Thread goldtech
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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for a unicode string

2007-10-25 Thread Thorsten Kampe
* 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for a unicode string

2007-10-24 Thread Martin v. Löwis
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for a unicode string

2007-10-24 Thread Scott David Daniels
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(thestring)) >= 128:
 print 'whatever you want'

-Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Test for a unicode string

2007-10-24 Thread Martin Marcher
2007/10/24, goldtech <[EMAIL PROTECTED]>:
> 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Test for a unicode string

2007-10-24 Thread goldtech
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

-- 
http://mail.python.org/mailman/listinfo/python-list