471,338 Members | 1,001 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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.
Dec 31 '07 #1
10 2847
On Dec 31, 2:20 am, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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) < 128)
Dec 31 '07 #2
On Dec 31, 1:20*pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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
Dec 31 '07 #3
On Dec 31, 7:20 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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?

Dec 31 '07 #4
On Dec 31, 7:20 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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?

Dec 31 '07 #5
On Dec 31, 2:54*am, abhishek <guptaabhishek1...@gmail.comwrote:
>
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
Dec 31 '07 #6
"si***************@gmail.com" <si***************@gmail.comwrote:
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'
Dec 31 '07 #7
On Mon, 31 Dec 2007 01:09:09 -0800, John Machin wrote:
On Dec 31, 7:20 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
>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
Dec 31 '07 #8
On Dec 31, 7:20 pm, "silverburgh.me...@gmail.com"
<silverburgh.me...@gmail.comwrote:
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

Dec 31 '07 #9
si***************@gmail.com 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.
Dec 31 '07 #10
Hallöchen!

Paul McGuire writes:
On Dec 31, 2:54*am, abhishek <guptaabhishek1...@gmail.comwrote:
>>
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>
Stupid me! How could I miss such a lovely feature in the language?

Tschö,
Torsten.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for further contact info.)
Dec 31 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Reply Via Newsgroup | last post: by
32 posts views Thread by Tubs | last post: by
10 posts views Thread by Mavenos | last post: by
22 posts views Thread by Terry Olsen | last post: by
1 post views Thread by arsaral | last post: by
121 posts views Thread by swengineer001 | last post: by
reply views Thread by rosydwin | last post: by

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.