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

S

silverburgh.meryl

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.
 
D

Dan Bishop

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)
 
A

abhishek

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
 
J

John Machin

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?
 
J

John Machin

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?
 
P

Paul McGuire

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
 
D

Duncan Booth

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:
'£42'
 
S

Steven D'Aprano

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.
 
J

John Machin

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
 
M

Michael Ströder

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.
 
T

Torsten Bronger

Hallöchen!

Paul said:
<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top