Removing Control characters

Amol Sonaikar amol at kenati.com
Mon Jan 6 06:31:59 EST 2003


Hi

In your reply below you have used chr(8) to identify ^H. Can I use same 
function chr(8) in if statement
for e.g. Similar like  
if line[i] == chr(8):
	i=i+1
else:
<  something >

Currently I am getting following error. What can be the correct syntax

    if str1[i] == chr(8)
                       ^
SyntaxError: invalid syntax


On Thursday 02 January 2003 08:31 pm, you wrote:
> Amol Sonaikar wrote:
> > I have an output of an command line operation which is stored in file and
> > it contains special characters. The file looks as below:
> >
> > --------------------------------------------------------------
> >
> > --->TestCase Command: add-route dest-ip subnet 22.12.23.44 subnetmask
> > 255.255.255.255 nexthop-ip 192.168.22.98
> > interface ppd0^G
> > iprouting> add-route dest-ip subnet 22.12.23.44 subnetmask
> > 255.255.255.255
> > ^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^
> >H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H^H$dest-ip
>
> ...etc...
>
> Judging from the subject line, you want to remove these control characters.
> Here goes:
>
>    # read all data as one big string
>    f = open(filename, "rb")
>    data = f.read()
>
>    # remove ^H
>    import string
>    data = string.replace(data, chr(8), "")
>
>    # do something nice with data, e.g. write it back to file,
>    # or display it, etc.
>
> HTH,





More information about the Python-list mailing list