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. 10 2947
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)
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
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?
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?
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
"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'
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
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 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.
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.) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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. ...
|
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...
|
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...
|
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)...
|
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...
|
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
...
|
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...
|
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))
{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |