Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Is there a string function to trim all non-ascii characters out of astring

Reply
Thread Tools

Is there a string function to trim all non-ascii characters out of astring

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      12-31-2007
Hi,

Is there a string function to trim all non-ascii characters out of a
string?
Let say I have a string in python (which is utf8 encoded), is there a
python function which I can convert that to a string which composed of
only ascii characters?

Thank you.
 
Reply With Quote
 
 
 
 
Dan Bishop
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 2:20 am, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>
> Thank you.


def ascii_chars(string):
return ''.join(char for char in string if ord(char) < 12
 
Reply With Quote
 
 
 
 
abhishek
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 1:20*pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>
> Thank you.


Use this function --

def omitNonAscii(nstr):
sstr=''
for r in nstr:
if ord(r)<127:
sstr+=r
return sstr
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 7:20 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>



 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 7:20 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>


OK, I'll bite: why do you want to throw data away?

 
Reply With Quote
 
Paul McGuire
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 2:54*am, abhishek <(E-Mail Removed)> wrote:
>
> Use this function --
>
> def omitNonAscii(nstr):
> * * sstr=''
> * * for r in nstr:
> * * * * if ord(r)<127:
> * * * * * * sstr+=r
> * * return sstr


<Yoda>
Learn the ways of the generator expression you must.
</Yoda>
See Dan Bishop's post.

-- Paul
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      12-31-2007
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>
> Thank you.


Yes, just decode it to unicode (which you should do as the first thing for
any encoded strings) and then encode it back to ascii with error handling
set how you want:

>>> s = '\xc2\xa342'
>>> s.decode('utf8').encode('ascii', 'replace')

'?42'
>>> s.decode('utf8').encode('ascii', 'ignore')

'42'
>>> s.decode('utf8').encode('ascii', 'xmlcharrefreplace')

'£42'
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      12-31-2007
On Mon, 31 Dec 2007 01:09:09 -0800, John Machin wrote:

> On Dec 31, 7:20 pm, "(E-Mail Removed)"
> <(E-Mail Removed)> wrote:
>> Hi,
>>
>> Is there a string function to trim all non-ascii characters out of a
>> string?
>> Let say I have a string in python (which is utf8 encoded), is there a
>> python function which I can convert that to a string which composed of
>> only ascii characters?
>>
>>

> OK, I'll bite: why do you want to throw data away?


Maybe he has to send the data to a device that can't deal with more than
7-bit ASCII.

Maybe he's sick of seeing text with "missing character" squares all over
from all the characters that his fonts can't display.

Maybe the string ends up as a file name on an operating system that
doesn't support unicode.

Or maybe he's just a curmudgeon who thinks life was better when there
were only 128 characters available.


--
Steven
 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-31-2007
On Dec 31, 7:20 pm, "(E-Mail Removed)"
<(E-Mail Removed)> wrote:
> Hi,
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?
>


You actually asked TWO different questions, and have got answers
mainly to the first one. Here's a very simple answer to the second
question, which has the advantage of no loss of information:

repr(your_utf8_string.decode('utf8'))
or merely
repr(your_utf8_string)

Cheers,
John

 
Reply With Quote
 
Michael Ströder
Guest
Posts: n/a
 
      12-31-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> Is there a string function to trim all non-ascii characters out of a
> string?
> Let say I have a string in python (which is utf8 encoded), is there a
> python function which I can convert that to a string which composed of
> only ascii characters?


I'd recommend to rethink this approach.
In the worst case the result is an empty string...

Ciao, Michael.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting a specific sequence of characters (linebreaks?) from astring Aldric Giacomoni Ruby 0 02-18-2009 03:57 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 4 01-30-2008 05:12 PM
How to call function whose function call with arguments is in astring grbgooglefan C++ 2 01-30-2008 07:18 AM
How to call function whose function call with arguments is in astring Options grbgooglefan C Programming 0 01-30-2008 04:19 AM
FAQ Topic - How do I trim whitespace - LTRIM/RTRIM/TRIM? FAQ server Javascript 0 08-28-2006 11:00 PM



Advertisments