Clean "Durty" strings

rzed rzantow at gmail.com
Mon Apr 2 08:54:07 EDT 2007


"Diez B. Roggisch" <deets at nospam.web.de> wrote in
news:57bt02F2c9fpuU2 at mid.uni-berlin.de: 

> Ulysse wrote:
> 
>> Hello,
>> 
>> I need to clean the string like this :
>> 
>> string =
>> """
>> bonne mentalité mec!:) \n                        <br>bon
>> pour info moi je suis un serial posteur arceleur dictateur ^^*
>> \n                        <br>mais pour avoir des resultats
>> probant il faut pas faire les mariolles, comme le
>> "fondateur" de bvs krew \n
>> mais pour avoir des resultats probant il faut pas faire les
>> mariolles, comme le "fondateur" de bvs krew \n
>> """
>> 
>> into :
>> bonne mentalité mec!:) bon pour info moi je suis un serial
>> posteur arceleur dictateur ^^* mais pour avoir des resultats
>> probant il faut pas faire les mariolles, comme le "fondateur"
>> de bvs krew mais pour avoir des resultats probant il faut pas
>> faire les mariolles, comme le "fondateur" de bvs krew
> 
> The obvious way that has been suggested to you at other places
> is to use BeautifulSoup.
>  
>> To do this I wold like to use only strandard librairies.
> 
> Then you need to reprogram what BeautifulSoup does. Happy
> hacking! 
> 

If the OP is constrained to standard libraries, then it may be a 
question of defining what should be done more clearly. The extraneous 
spaces can be removed by tokenizing the string and rejoining the 
tokens. Replacing portions of a string with equivalents is standard 
stuff. It might be preferable to create a function that will accept 
lists of from and to strings and translate the entire string by 
successively applying the replacements. From what I've seen so far, 
that would be all the OP needs for this task. It might take a half-
dozen lines of code, plus the from/to table definition.

-- 
rzed




More information about the Python-list mailing list