Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > RE: ascii character - removing chars from string

Reply
Thread Tools

RE: ascii character - removing chars from string

 
 
bruce
Guest
Posts: n/a
 
      07-04-2006
simon...

the issue that i'm seeing is not a result of simply using the
'string.replace' function. it appears that there's something else going on
in the text....

although i can see the nbsp in the file, the file is manipulated by a number
of other functions prior to me writing the information out to a file.
somewhere the 'nbsp' is changed, so there's something else going on...

however, the error i get indicates that the char 'u\xa0' is what's causing
the issue.. as far as i can determine, the string.replace can't/doesn't
handle non-ascii chars. i'm still looking for a way to search/replace
non-ascii chars...

this would/should resolve my issue..

-bruce


-----Original Message-----
From: python-list-bounces+bedouglas=
[mailtoython-list-bounces+bedouglas=]On Behalf
Of Simon Forman
Sent: Monday, July 03, 2006 11:28 PM
To: python-
Subject: Re: ascii character - removing chars from string


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: python-list-bounces+bedouglas=
> [mailtoython-list-bounces+bedouglas=]On Behalf
> Of Simon Forman
> Sent: Monday, July 03, 2006 7:17 PM
> To: python-
> 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


--
http://mail.python.org/mailman/listinfo/python-list

 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-04-2006
On Tue, 04 Jul 2006 08:09:53 -0700, bruce wrote:

> simon...
>
> the issue that i'm seeing is not a result of simply using the
> 'string.replace' function. it appears that there's something else going on
> in the text....
>
> although i can see the nbsp in the file, the file is manipulated by a number
> of other functions prior to me writing the information out to a file.
> somewhere the 'nbsp' is changed, so there's something else going on...
>
> however, the error i get indicates that the char 'u\xa0' is what's causing
> the issue..


As you have written it, that's not a character, it is a string of length
two. Did you perhaps mean the Unicode character u'\xa0'?

>>> len('u\xa0')

2
>>> len(u'\xa0')

1


> as far as i can determine, the string.replace can't/doesn't
> handle non-ascii chars. i'm still looking for a way to search/replace
> non-ascii chars...


Seems to work for me:

>>> c = u'\xa0'
>>> s = "hello " + c + " world"
>>> s

u'hello \xa0 world'
>>> s.replace(c, "?")

u'hello ? world'



--
Steven.

 
Reply With Quote
 
 
 
 
Fredrik Lundh
Guest
Posts: n/a
 
      07-04-2006
bruce wrote:

> i've done the s.replace('\xa0','') with no luck.


let me guess: you wrote

s.replace("\xa0", "")

instead of

s = s.replace("\xa0", "")

?

</F>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
removing ASCII escape chars from output SuperGh0d@gmail.com Perl Misc 3 12-24-2007 06:33 PM
Regex with ASCII and non-ASCII chars TOXiC Python 5 01-31-2007 04:48 PM
RE: ascii character - removing chars from string bruce Python 1 07-04-2006 04:34 PM
RE: ascii character - removing chars from string update bruce Python 1 07-04-2006 06:54 AM
ascii character - removing chars from string bruce Python 6 07-04-2006 06:28 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57