Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; "'',": 0.07; 'element': 0.07; 'intermediate': 0.07; 'none,': 0.07; 'skip:\\ 20': 0.07; 'string': 0.09; 'converted': 0.09; 'immutable': 0.09; 'spaces': 0.09; 'python': 0.11; 'newline,': 0.16; 'newlines': 0.16; 'retrived': 0.16; 'str)': 0.16; 'subject:Remove': 0.16; 'tab': 0.16; 'tuple': 0.16; 'tuple,': 0.16; 'unchanged': 0.16; 'elements': 0.16; 'appropriate': 0.16; 'all,': 0.19; "skip:' 30": 0.19; 'meant': 0.20; '>>>': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; '>>>': 0.24; 'format,': 0.24; '---': 0.24; 'header:In-Reply-To:1': 0.27; '----': 0.29; 'characters': 0.30; 'needed.': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; '"': 0.31; 'container': 0.31; 'this.': 0.32; 'regular': 0.32; 'url:python': 0.33; '(e.g.': 0.33; 'subject:from': 0.34; 'could': 0.34; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'list': 0.37; 'e.g.': 0.38; 'handle': 0.38; 'needed': 0.38; 'to:addr:python- list': 0.38; 'to:addr:python.org': 0.39; 'space': 0.40; 'url:mail': 0.40; 'remove': 0.60; 'expression': 0.60; 'skip:t 30': 0.61; 'new': 0.61; 'skip:n 10': 0.64; 'more': 0.64; "'3',": 0.84; 'hi,you': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=TzCLhsBWQXMGxFkae0XZuqnYFqLWAQeamKAM5Asfp4g=; b=Ze0MTOUNDFsv/f27q/vo2qQpbgYtOhSeq2BIcuPP6C6Yw4EGaXIFIWn1uwN2xMAqEW 9APDvqdC5qfV5vUub4kGN7SJjzicNoejSj7jlQ+o/rX8NJ4DT4HMZjpJSMh3MrOQtTeK ueFVlLq1c5JPb9LCHGUlyFUi+Lz0s3GnetGYwiXz5xKIyn7YXbhn3KJLp9TsjxBcvzRH 9aFH56vaND8or3HheQ2NRO27lrm19+aWZOdMRGKmpR4tlcaJ0QYZogrT8xAmJb2xq0tm Syv07Zfb+jrZo3ikwfTFHyNMejc23NXO+Vh1G3bMWGmKSyTJmY82K25K15BnfDGXdr3p b67Q== MIME-Version: 1.0 X-Received: by 10.60.115.164 with SMTP id jp4mr9542221oeb.19.1379497849346; Wed, 18 Sep 2013 02:50:49 -0700 (PDT) In-Reply-To: References: Date: Wed, 18 Sep 2013 11:50:49 +0200 Subject: Re: Remove \n, " " from tuple. From: Vlastimil Brom To: python Content-Type: multipart/alternative; boundary=089e01161b4647adde04e6a55f25 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 139 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1379497852 news.xs4all.nl 15969 [2001:888:2000:d::a6]:51047 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:54366 --089e01161b4647adde04e6a55f25 Content-Type: text/plain; charset=ISO-8859-1 2013/9/18 Venkat Addala > 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 > 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 --089e01161b4647adde04e6a55f25 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable

2013/9/18 Venkat Addala <venkat.boffin@gmail.com>
H= i all,=A0

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\nghtscety= lsse', '3', '3q25.1', 151531861L, 151546276L, '+= 9;, '1 kjafhkfhlad\fdfdsdf ghtscetylsse \ncontends the sctivity of dsfd= k srylsmine N-scetyltrsnsfersse thst scts ss s cstslyst inbiotrsnsformstion= fghjfg for srylsmine snd bvhghbh smine csrcinogens. Bgskjdg in ssDsC dfkdk= df in bresst snd prostrste csncer.', '', 'Msdhurs \nsgdfgss= ssvslingegowds', dstetime.dste(2013, 6, 14), None



--
Regards

<= span>Venkat Addala=A0
Computational = Biologist
=A0 O__ =A0----
=A0_/ /`_ ---
(*) \(*) --



--
https://mail.python.org/mailman/listinfo/python-list

Hi,
you can create a new tuple with the con= verted 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+&= quot;, " ", element) if isinstance(element, str) else element for= element in ("qw e", 1, 2, None, "U\nN IC", &#= 39;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, othe= rs are left unchanged
=A0if isinstance(element, str) is meant for= =A0python 3, for python 2=A0 if isinstance(element, basestring) would be m= ore appropriate
the elements are collected in a list using this list comprehension
and this intermediate list is finaly converted to a tuple if this im= mutable container is needed.

hth,
=A0 vb= r

--089e01161b4647adde04e6a55f25--