473,671 Members | 2,206 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Fastest way is probably with the built-in string.translat e 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\x00ti ti" -> "tototiti" or " toto titi"


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

s = "\x08toto\x00ti ti"
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\x00ti ti" -> "tototiti" or " toto titi"


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

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\x00ti ti"

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
20644
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 applications." Although I have seen this before when trying to remove .NET assemblies that have been installed via an MSI package, I now get this message while trying to remove any assembly I've added to the GAC. Using gacutil does not work...
1
2377
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 bad, except for the fact that I need to keep track of a file's associated name in addition to it's fstream reference (I'm presuming that fstream can not give me the filename, don't see a reference to this either, please correct me if necessary) in...
11
3070
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 class for a primitive int with some methods. I don't show the Integer class because it will not add any information to my problem. Main is using some STL function Now to my problem.
12
5214
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 style attributes that I don't want to remove.
7
9316
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 modified... I understand this, since I remove one object from collection...
6
11123
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 RowDataBound event, I can find the button, but can't seem to remove it. The code below does not throw any exception, but the button is not removed. Protected Sub gv1_RowDataBound(ByVal sender As Object, ByVal e As
3
2623
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 calling myDataTable.Rows.Remove(myRow) have no effect on it. What am I missing? I tried to add the row and then delete and then call AcceptChanges but no success.
11
4496
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 rows inserted/sec up to 500, and 100rows/6secs up to 3000, which isn't great but then the Row Count is clicking away for the user to see and they can hit the "cancel" button at anytime, so overall I'm happy), what really disappoints me is the...
10
2654
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 in a simple case. Analysis of the code suggests that the only possible explanation is that the following occurs, os.remove(x) ..... stuff if os.path.isfile(x):
3
5259
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 add the pane the remove event does not get handled, though thereafter it is handled without problems. ==================================================
0
8472
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8390
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8819
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8667
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6222
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4221
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4399
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2048
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.