remove special characters from line

Bob Gailer bgailer at alum.rpi.edu
Tue Jul 1 15:22:17 EDT 2003


At 10:03 AM 7/1/2003 -0500, Chris Rennert wrote:
>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.

Please define "special character". You have given us one example, but 
that's not sufficient to guess your meaning.

One way to do this is to create a string with all acceptable characters, 
then see if the first character of each line is in that string.

Consider string.printable:
 >>> import string
 >>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ 
\t\n\r\x0b\x0c'
If you don't want the control characters at the end, use string.printable[:95]

for line in file(filename).readlines():
   if line[0] not in string.printable:
     del line[0]
     ... process line

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625
-------------- next part --------------

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.492 / Virus Database: 291 - Release Date: 6/24/2003


More information about the Python-list mailing list