473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Remove no-printable characters in string

Hello,

What's the best way to delete or replace no-printable characters in a string.
i.e.: "\x08toto\x00titi" -> "tototiti" or " toto titi"
Jul 18 '05 #1
3 2476
pa***********@free.fr (Pascal) writes:
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.
Jul 18 '05 #2
Pascal wrote:
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'

Jul 18 '05 #3
Pascal wrote:
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

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Peter Gomis | last post by:
I have encountered a situation where I am unable to remove a .NET assembly from the GAC. The message I receive is "Assembly 'assemblyname' could not be uninstalled because it is required by other...
1
by: Matt | last post by:
Hello, I would like to be able to delete (as in stdio.h remove()) fstream objects. Is there a means for me to do this in C++? I see no such capability in my references. remove() is not so...
11
by: Tony Johansson | last post by:
Hello! I have some problem with STL function remove I have two classes called Handle which is a template class and Integer which is not a template class. The Integer class is just a wrapper...
12
by: Oberon | last post by:
I have a large HTML document. It has hundreds of <span>s which have no attributes so these <span>s are redundant. How can I remove these tags automatically? The document also has <span>s with...
7
by: Franck Diastein | last post by:
Hi, I'm trying to remove items from a collection this way: foreach(Object myO in ObjectCol){ if(myO != "xxx"){ myO.Remove(); } } But I'm having an error telling me that Collection was...
6
by: AG | last post by:
I have a gridview with a template column containing an imagebutton to delete the row. Under some condition I don't want the row to be deleted, so would like to remove the button. In the...
3
by: Hamed | last post by:
Hello I have a DataTable bound to a DataGrid. During working with the grid, some detached rows are created internally by the grid. I want to remove the detached rows from the DataTable but...
11
by: Richard Maher | last post by:
Hi, I have read many of the copius entries on the subject of IE performance (or the lack thereof) when populating Select Lists. I don't mind the insert performance so much, (I get 100x120byte...
10
by: ppaterson | last post by:
Can os.path.isfile(x) ever return True after os.remove(x) has successfully completed? (Windows 2003, Python 2.3) We had a couple of failures in a server application that we cannot yet reproduce...
3
by: Allen Chen [MSFT] | last post by:
Hi Richard, Quote from Richard================================================== However I also want to be able to remove the panes. I have tried to include this, but find that when I first...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.