Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: remove special characters from line

Reply
Thread Tools

Re: remove special characters from line

 
 
Chris Rennert
Guest
Posts: n/a
 
      07-01-2003
I apologize for being vague on what a "special" character is in my context.
For me, it would be anything that isn't A..Z , a..z, or 0..9 .
My list seems to work, or like it was suggested I could have used a string
as well. I thank everyone for the help, and I am really enjoying learning
Python.

Thanks again,
Chris Rennert
"Chris Rennert" <(E-Mail Removed)> wrote in message
news:3f01a27d$0$43847$(E-Mail Removed). ..
> Hello all,
>
> If I have a line like this
>
> ˛blah blah blah blah blah
>
> I know I could do a slice like this [1:] to pull everything but the

special
> character, but what if I have several lines in a file.
> I am not sure how I would detect a special character like that. I would
> just like to pull everything from those lines (and the special character
> always appears as the first character, but not on every line) except for

the
> special characters.
> I hope I have enough detail for someone to help me.
>
> Thanks in advance,
>
> Chris
>
>



 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      07-02-2003
"Chris Rennert" <(E-Mail Removed)> wrote in message news:<3f01e0b0$0$43854$(E-Mail Removed) >...
> I apologize for being vague on what a "special" character is in my context.
> For me, it would be anything that isn't A..Z , a..z, or 0..9 .
> My list seems to work, or like it was suggested I could have used a string
> as well. I thank everyone for the help, and I am really enjoying learning
> Python.
>


That's good news ... and here's a couple of more lessons:

(1) You can use the translate method:

# once
import string
id_trans = "".join([chr(x) for x in range(256)])
good_chars = string.letters + string.digits
bad_chars = "".join([x for x in id_trans if x not in good_chars])
# then once for each maybe_bad_string
good_string = maybe_bad_string.translate(id_trans, bad_chars)

That is a bit over the top, but reading the doc to understand what is
going down will pay dividends.

(2) Here's a more straightforward way using the re module

# once
import re
subber = re.compile(r"[^A-Za-z0-9]").sub
# then once for each maybe_bad_string
good_string = subber("", maybe_bad_string)
# simpler but slower:
good_string = re.sub(r"[^A-Za-z0-9]", "", maybe_bad_string)
# and as a bonus extra
show_where_junk_was_string = subber("?", maybe_bad_string)

HTH,
John
 
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
Remove only special characters and junk characters from a file rvino Perl 0 08-14-2007 07:23 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
Re: remove special characters from line Bob Gailer Python 0 07-01-2003 07:22 PM
Re: remove special characters from line Egor Bolonev Python 2 07-01-2003 04:15 PM
Re: remove special characters from line Radovan Garabik Python 0 07-01-2003 03:14 PM



Advertisments