Remove no-printable characters in string

P

Pascal

Hello,

What's the best way to delete or replace no-printable characters in a string.
i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"
 
P

Paul Rubin

What's the best way to delete or replace no-printable characters in a string.
i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"

Fastest way is probably with the built-in string.translate operation.
 
R

Roel Mathys

Pascal said:
Hello,

What's the best way to delete or replace no-printable characters in a string.
i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"

import string
import sets
printable = sets.Set( string.printable )

s = "\x08toto\x00titi"
t = ''.join( [ i for i in s if i in printable or i.isalpha() ] )

=> t == 'tototiti'

or

replace = { '\x00' : ' ' }
t = ''.join( [ replace.get(i,i) for i in s
if i in printable
or i.isalpha() # to catch for example 'ë'
or i in replace ] )

=> t == 'toto titi'
 
R

Roel Mathys

Pascal said:
Hello,

What's the best way to delete or replace no-printable characters in a string.
i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"

import string
import sets
printable = sets.Set( string.printable )



def f1( s ) :
return ''.join( [ i for i in s if i in printable or i.isalpha() ] )

def f2( s ) :
replace = { '\x00' : ' ' }
t = ''.join(
[ replace.get(i,i)
for i in s
if i in printable
or i.isalpha() # for non ascii characters
or i in replace
] )

s = "\x08toto\x00titi"

t1 = f1(s) => 'tototiti'
t2 = f2(s) => 'toto titi'

works nicely, I think

bye,
roel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top