2013/9/18 Venkat Addala <venkat.bof...@gmail.com> > Hi all, > > I have a tuple in this format, which is retrived using MySQLdb, now i want > to remove \n and extra spaces in this. > > 13L, 'ssDsC', 'CEs5s1, DsC', 'srylscetsmight\nghtscetylsse', '3', > '3q25.1', 151531861L, 151546276L, '+', '1 kjafhkfhlad\fdfdsdf ghtscetylsse > \ncontends the sctivity of dsfdk srylsmine N-scetyltrsnsfersse thst scts ss > s cstslyst inbiotrsnsformstion fghjfg for srylsmine snd bvhghbh smine > csrcinogens. Bgskjdg in ssDsC dfkdkdf in bresst snd prostrste csncer.', '', > 'Msdhurs \nsgdfgssssvslingegowds', dstetime.dste(2013, 6, 14), None > > > > -- > Regards > Venkat Addala <http://in.linkedin.com/in/venkataddala/> > Computational Biologist > O__ ---- > _/ /`_ --- > (*) \(*) -- > > > > -- > https://mail.python.org/mailman/listinfo/python-list > > Hi, you can create a new tuple with the converted elements from the original one using a list comprehension and the regular expression replacement, it could be e.g.
>>> import re >>> tuple([re.sub(r"\s+", " ", element) if isinstance(element, str) else element for element in ("qw e", 1, 2, None, "U\nN IC", 'Q\tW E')]) ('qw e', 1, 2, None, 'U N IC', 'Q W E') >>> re.sub(...) replaces one or more whitespace characters (space, newline, tab ...) with a single space - the pattern can be tweaked as needed (e.g. the pattern r" *\n *| +" would only handle spaces and newlines explicitely). This replacement is only applied on string elements of the tuple, others are left unchanged if isinstance(element, str) is meant for python 3, for python 2 if isinstance(element, basestring) would be more appropriate the elements are collected in a list using this list comprehension and this intermediate list is finaly converted to a tuple if this immutable container is needed. hth, vbr
-- https://mail.python.org/mailman/listinfo/python-list