473,728 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ascii character - removing chars from string

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_s tr)

where 'strip' removes/strips out the invalid chars...

any ideas/thoughts/pointers...

thanks

-bruce

Jul 3 '06 #1
6 8659
bruce:
valid_str = strip(invalid_s tr)
where 'strip' removes/strips out the invalid chars...
This isn't short but it is fast:
import string
valid_chars = string.lowercas e + string.uppercas e + \
string.digits +
"""|!'\\"$ %&/()=?^*_:;>+,.-<\n \t"""
all_chars = "".join(map ( chr, range(256)) )
comp_valid_char s = "".join( set(all_chars). difference(vali d_chars) )
print "test string".transla te(all_chars, comp_valid_char s)
Shorter and a bit slower alternative:
import string
valid_chars_set = set(string.lowe rcase + string.uppercas e
+ 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

Jul 3 '06 #2
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?
Jul 4 '06 #3
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_s tr)

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_cha rs = 'abc'
a_string.stri p(invalid_chars )
'def'

Jul 4 '06 #4
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_s tr)

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.

Jul 4 '06 #5
bruce wrote:
hi...

update. i'm getting back html, and i'm getting strings like " foo &nbsp;"
which is valid HTML as the '&nbsp;' is a space.
&, n, b, s, p, ; Those are all ascii characters.
i need a way of stripping/removing the '&nbsp;' from the string

the &nbsp; needs to be treated as a single char...

text = "foo cat &nbsp;"

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 &nbsp;'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"

Jul 4 '06 #6
bruce wrote:
simon...

the '&nbsp;' 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 '&nbsp;', happen
to represent non-ascii values.
>
i'm looking to remove or replace the insances with a ' ' (space)
Simplicity:

s.replace('&nbs p;', ' ')

~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************* *************** *************@p ython.org
[mailto:py****** *************** *************** *****@python.or g]On Behalf
Of Simon Forman
Sent: Monday, July 03, 2006 7:17 PM
To: py*********@pyt hon.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 &nbsp;"
which is valid HTML as the '&nbsp;' is a space.

&, n, b, s, p, ; Those are all ascii characters.
i need a way of stripping/removing the '&nbsp;' from the string

the &nbsp; needs to be treated as a single char...

text = "foo cat &nbsp;"

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 &nbsp;'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
Jul 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
12486
by: Daniel | last post by:
Hi, is there a way to check if a letter entered is an uppercase ASCII character? Thanks Daniel
12
70543
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!). Could anyone help me out? Thanks Dave
2
13209
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
1
4376
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 the text (& these two formating characters) by substituting other characters for them. After substitutng for them, the new substitutions appear as they should, however the text still behaves as if there is a either a carriage return or a new...
2
7692
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. But now, two things can be occured since the last time i used it. the first one, something concerning to the encoding has changed and i haven't noticed it.
5
2680
by: Isa Janfada | last post by:
Hello, I have a html text string like this: " When&nbsp; creating&nbsp; a&nbsp; new&nbsp; message,&nbsp; Reset&nbsp; occurs&nbsp; when&nbsp; '\'&nbsp; is&nbsp; entered&nbsp; in&nbsp; Address.&nbsp; &nbsp; &nbsp; S-1a<BR>᡼ɥ쥹\ȥꥻåȤ롡 How can I find and remove the strings like
9
8259
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
6
28105
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 places. So far, I cannot find anything that works. My application gets a string of characters from an external device via the serial port. I can use the substring method to get just one character from that input string, and I need to be able to convert...
13
17608
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 code project which kind of looks like it does what I want but I can't help thinking it makes it more complex than it needs to be.
0
8904
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9424
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9277
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6013
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4524
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3236
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.