2010/4/16 Daniele Varrazzo <p...@develer.com> > On Fri, 16 Apr 2010 11:11:03 +0200, Valerio Pachera <siri...@gmail.com> > wrote: > > Avrei bisogno di una cosa tipo > > > > stringa = ' questa stringa ha tabulazioni e più spazi nel mezzo e > > / alla fine " > > > > print stringa.replace('\t', '').strip() > > > > questa concatenazione rimuove tutte le taulazione e tutti gli spazi ad > > _inizio_ e _fine_ stringa. > > Vorrei essere sicuro di lasciare un solo spazio fra le parole. > > In [1]: s = " questa stringa ha tabulazioni \t e piu' spazi nel mezzo e > alla fine " > > In [2]: " ".join(s.split()) > Out[2]: "questa stringa ha tabulazioni e piu' spazi nel mezzo e alla fine" >
Ma dai? Grandissimo Piro! E molto più pythonico. Non lo sapevo che lo split prendeva di default anche i tab: split(...) S.split([sep [,maxsplit]]) -> list of strings Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result. Ciao. Marco. -- http://thinkcode.tv/gratis - Capire in 15 minuti cosa può fare Python http://stacktrace.it - Aperiodico di resistenza informatica http://python.thinkcode.tv - Videocorso di Python http://beri.it - Blog di una testina di vitello
_______________________________________________ Python mailing list Python@lists.python.it http://lists.python.it/mailman/listinfo/python