hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii chars.
i'm assuming python has something like
valid_str = strip(invalid_str)
where 'strip' removes/strips out the invalid chars...
any ideas/thoughts/pointers...
thanks
-bruce 6 8595
bruce:
valid_str = strip(invalid_str)
where 'strip' removes/strips out the invalid chars...
This isn't short but it is fast:
import string
valid_chars = string.lowercase + string.uppercase + \
string.digits +
"""|!'\\"£$%&/()=?^*é§_:;>+,.-<\n \t"""
all_chars = "".join(map( chr, range(256)) )
comp_valid_chars = "".join( set(all_chars).difference(valid_chars) )
print "test string".translate(all_chars, comp_valid_chars)
Shorter and a bit slower alternative:
import string
valid_chars_set = set(string.lowercase + string.uppercase
+ string.digits +
"""|!'\\"£$%&/()=?^*é§_:;>+,.-<\n \t""")
print filter(lambda c: c in valid_chars_set, "test string")
You can add the chars you want to the string of accepted ones.
Bye,
bearophile
On 4/07/2006 9:27 AM, bruce wrote:
hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii chars.
It's possible that you would be better off handling those characters in
some fashion other than blowing them away. What are the characters that
you are seeing, and what is the problem that they are causing you?
bruce wrote:
hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii chars.
i'm assuming python has something like
valid_str = strip(invalid_str)
where 'strip' removes/strips out the invalid chars...
any ideas/thoughts/pointers...
If you're able to define the invalid_chars, the most convenient is
probably to use the strip() method:
>>a_string = "abcdef" invalid_chars = 'abc' a_string.strip(invalid_chars)
'def'
bruce wrote:
hi...
i'm running into a problem where i'm seeing non-ascii chars in the parsing
i'm doing. in looking through various docs, i can't find functions to
remove/restrict strings to valid ascii chars.
i'm assuming python has something like
valid_str = strip(invalid_str)
where 'strip' removes/strips out the invalid chars...
any ideas/thoughts/pointers...
thanks
-bruce
You might be able to use the translate() and maketrans() string
methods. See http://groups.google.ca/group/comp.l...02a21c95bd0ec9
second-to-last post for an example.
bruce wrote:
hi...
update. i'm getting back html, and i'm getting strings like " foo "
which is valid HTML as the ' ' is a space.
&, n, b, s, p, ; Those are all ascii characters.
i need a way of stripping/removing the ' ' from the string
the needs to be treated as a single char...
text = "foo cat "
ie ok_text = strip(text)
ok_text = "foo cat"
Do you really want to remove those html entities? Or would you rather
convert them back into the actual text they represent? Do you just
want to deal with 's? Or maybe the other possible entities that
might appear also?
Check out htmlentitydefs.entitydefs (see http://docs.python.org/lib/module-htmlentitydefs.html) it's kind of
ugly looking so maybe use pprint to print it:
>>import htmlentitydefs, pprint pprint.pprint(htmlentitydefs.entitydefs)
{'AElig': 'Æ',
'Aacute': 'Á',
'Acirc': 'Â',
..
..
..
'nbsp': '\xa0',
..
..
..
etc...
HTH,
~Simon
"You keep using that word. I do not think it means what you think it
means."
-Inigo Montoya, "The Princess Bride"
bruce wrote:
simon...
the ' ' is not to be seen/viewed as text/ascii.. it's a representation
of a hex 'u\xa0' if i recall...
Did you not see this part of the post that you're replying to?
'nbsp': '\xa0',
My point was not that '\xa0' is an ascii character... It was that your
initial request was very misleading:
"i'm running into a problem where i'm seeing non-ascii chars in the
parsing i'm doing. in looking through various docs, i can't find
functions to remove/restrict strings to valid ascii chars."
That's why you got three different answers to the wrong question.
You weren't "seeing non-ascii chars" at all. You were seeing ascii
representations of html entities that, in the case of ' ', happen
to represent non-ascii values.
>
i'm looking to remove or replace the insances with a ' ' (space)
Simplicity:
s.replace(' ', ' ')
~Simon
"You keep using that word. I do not think it means what you think it
means."
-Inigo Montoya, "The Princess Bride"
>
-bruce
-----Original Message-----
From: py*****************************************@python .org
[mailto:py***************************************** @python.org]On Behalf
Of Simon Forman
Sent: Monday, July 03, 2006 7:17 PM
To: py*********@python.org
Subject: Re: ascii character - removing chars from string
bruce wrote:
hi...
update. i'm getting back html, and i'm getting strings like " foo "
which is valid HTML as the ' ' is a space.
&, n, b, s, p, ; Those are all ascii characters.
i need a way of stripping/removing the ' ' from the string
the needs to be treated as a single char...
text = "foo cat "
ie ok_text = strip(text)
ok_text = "foo cat"
Do you really want to remove those html entities? Or would you rather
convert them back into the actual text they represent? Do you just
want to deal with 's? Or maybe the other possible entities that
might appear also?
Check out htmlentitydefs.entitydefs (see http://docs.python.org/lib/module-htmlentitydefs.html) it's kind of
ugly looking so maybe use pprint to print it:
>import htmlentitydefs, pprint pprint.pprint(htmlentitydefs.entitydefs)
{'AElig': 'Æ',
'Aacute': 'Á',
'Acirc': 'Â',
.
.
.
'nbsp': '\xa0',
.
.
.
etc...
HTH,
~Simon
"You keep using that word. I do not think it means what you think it
means."
-Inigo Montoya, "The Princess Bride"
-- http://mail.python.org/mailman/listinfo/python-list This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Daniel |
last post by:
Hi, is there a way to check if a letter entered is an uppercase ASCII
character?
Thanks
Daniel
|
by: David Williams |
last post by:
Hi all, i have been able to convert an ASCII character to an INT however im
lost as to how to change them back. Cant find anything on the net (though im
probably looking in the wrong places!)....
|
by: jt |
last post by:
Looking for an example how to convert and CString to an ASCII character
string.
Any examples on how to do this?
Thank you,
jt
|
by: Steve |
last post by:
Hello,
I'm running into a problems with a non-ascii "character" in a text
string. Im parsing character by character and all lines end with ascii
13 & 10 (Carriage return & newline). I'm encoding...
|
by: jau |
last post by:
Hi co-listers!
I have been off Python for 2 years and now, that i'm used to Eclipse and
Java, I decided to start a project with Python to refresh skills this
time using Eclipse and TrueStudio....
|
by: Isa Janfada |
last post by:
Hello,
I have a html text string like this:
" When creating a
new message, Reset occurs when '\'
is entered in ...
|
by: simchajoy2000 |
last post by:
Hi,
I know what the ASCII Character Codes are for the 2nd and 3rd powers in
VB.NET but I can't find the 6th power anywhere - does anyone know what
it might be or if it even exists?
Joy
|
by: davetelling |
last post by:
I am a total newbie, trying to slog through the Visual C# Express
application. I need to be able to convert a single ASCII character (can be
anything from 0 to 255) to an int for use in other...
|
by: Eps |
last post by:
Hi there,
I believe all strings in .net are unicode by default, I am looking for a
way to remove all non ascii characters from a string (or optionally
replace them).
There is an article on...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |