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

 
 
Bob Gailer
Guest
Posts: n/a
 
      07-01-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

'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~
\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
http://www.velocityreviews.com/forums/(E-Mail Removed)
303 442 2625


---
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

 
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 Chris Rennert Python 1 07-02-2003 01:05 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