Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #54366

Re: Remove \n, " " from tuple.

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 <vlastimil.brom@gmail.com>
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&gt;': 0.22; '&gt;&gt;&gt;': 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; '&quot;': 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 <CAAZP3kmUnKe2BnSK2vkXijjuYiQ3ZHFUY=6A9T1iUD7z1baSNw@mail.gmail.com>
References <CAAZP3kmUnKe2BnSK2vkXijjuYiQ3ZHFUY=6A9T1iUD7z1baSNw@mail.gmail.com>
Date Wed, 18 Sep 2013 11:50:49 +0200
Subject Re: Remove \n, " " from tuple.
From Vlastimil Brom <vlastimil.brom@gmail.com>
To python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.112.1379497852.18130.python-list@python.org> (permalink)
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

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

2013/9/18 Venkat Addala <venkat.boffin@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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Remove \n, " " from tuple. Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-09-18 11:50 +0200

csiph-web