473,396 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2964
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Reply Via Newsgroup | last post by:
Folks, In PHP and some other scripting languages, one has trim() - It removes newline, tabs and blank spaces that might prefix, or suffix a string. Can someone tell me how I can do this in...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
32
by: Tubs | last post by:
Am i missing something or does the .Net Framework have a quirk in the way methods work on an object. In C++ MFC, if i have a CString and i use the format method, i format the string i am using. ...
9
by: rsine | last post by:
I have developed a program that sends a command through the serial port to our business system and then reads from the buffer looking for a number. Everything worked great on my WinXP system, but...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
10
by: Mavenos | last post by:
Hi Web Masters, Just wondering wether you can help us to come up with some tokenize script. My problem is wanted to display a LONG content into a short para (by giving minimum letter lenght)...
22
by: Terry Olsen | last post by:
I have an app that makes decisions based on string content. I need to make sure that a string does not contain only spaces or newlines. I am using the syntax 'Trim(String)" and it works fine. I...
1
by: arsaral | last post by:
Hi, Here is an extension of MSDN's trim right example to trim left-right... First subroutines then the calling structure is given below... Cheers. Ali Riza SARAL ...
1
by: Sankalp | last post by:
Hi, I am using VB 2005. My application has many data bound controls. The connection is stored in the app.config file. I want the application to start with a default connection string and while...
121
by: swengineer001 | last post by:
Just looking for a few eyes on this code other than my own. void TrimCString(char *str) { // Trim whitespace from beginning: size_t i = 0; size_t j; while(isspace(str)) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...

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.