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


Groups > comp.lang.python > #107825

Re: Simplest way to locate a string in a column and get the value on the same row in another column

Path csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: Simplest way to locate a string in a column and get the value on the same row in another column
Date Thu, 28 Apr 2016 14:06:51 -0500
Lines 57
Message-ID <mailman.210.1461870637.32212.python-list@python.org> (permalink)
References <759417988.6710506.1461868599459.JavaMail.yahoo.ref@mail.yahoo.com> <759417988.6710506.1461868599459.JavaMail.yahoo@mail.yahoo.com> <1650815019.6612341.1461868675273.JavaMail.yahoo@mail.yahoo.com> <20160428140651.36ce5e63@bigbox.christie.dr>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de 80DgO7+QkD0NsDnohyE63Ao0815uL8AknnkLyxvADscA==
Return-Path <python.list@tim.thechases.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.007
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'column': 0.07; 'matches': 0.07; 'nicely': 0.07; 'table.': 0.07; 'fname': 0.09; 'subject:same': 0.09; 'subject:string': 0.09; 'headers': 0.15; 'subject: \n ': 0.15; '-tkc': 0.16; 'dict(': 0.16; 'ellen': 0.16; 'fname,': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'row': 0.16; 'wrote:': 0.16; 'string': 0.17; 'import': 0.24; 'header:In-Reply- To:1': 0.24; 'header': 0.24; "i've": 0.25; 'helpful': 0.27; 'define': 0.27; 'spaces': 0.29; 'field,': 0.30; 'another': 0.32; 'gives': 0.35; 'something': 0.35; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'files': 0.38; 'data': 0.39; 'subject:the': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'some': 0.40; 'john': 0.61; 'hope': 0.61; 'success': 0.62; 'miller': 0.63; 'our': 0.64; 'you.': 0.64; 'python-list': 0.66; 'forward': 0.66; 'hearing': 0.67; 'smith': 0.76; 'subject:get': 0.81; 'received:23': 0.84; 'subject:value': 0.84
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-Sender-Id wwwh|x-authuser|tim@thechases.com
X-MC-Relay Neutral
X-MailChannels-SenderId wwwh|x-authuser|tim@thechases.com
X-MailChannels-Auth-Id wwwh
X-MC-Loop-Signature 1461870633129:2101036818
X-MC-Ingress-Time 1461870633129
In-Reply-To <1650815019.6612341.1461868675273.JavaMail.yahoo@mail.yahoo.com>
X-Mailer Claws Mail 3.11.1 (GTK+ 2.24.25; x86_64-pc-linux-gnu)
X-AuthUser tim@thechases.com
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.22
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>
X-Mailman-Original-Message-ID <20160428140651.36ce5e63@bigbox.christie.dr>
X-Mailman-Original-References <759417988.6710506.1461868599459.JavaMail.yahoo.ref@mail.yahoo.com> <759417988.6710506.1461868599459.JavaMail.yahoo@mail.yahoo.com> <1650815019.6612341.1461868675273.JavaMail.yahoo@mail.yahoo.com>
Xref csiph.com comp.lang.python:107825

Show key headers only | View raw


On 2016-04-28 18:37, David Shi via Python-list wrote:
>  What is the simplest way to locate a string in a column and get
> the value on the same row in another column ? 1  a2  b3  c
> Locate b and obtain 2 in a table.
> Looking forward to hearing from you.

I've had success with using regexp matches with their start/stop
location information:

  s1 = "1  a2    b3  c"
  s2 = "abcdefghijklmn"

  import re
  r = re.compile(r"a2\s*") # define the pattern you want
  m = r.search(s1)
  if m: # if our pattern was in the header
    # take the corresponding slice out of s2
    result = s2[m.start():m.end()] # 'defghi'

This also works nicely with re.finditer() as I've had
column-delimited files where each header had no spaces

  EMPID    FIRST_NAME     LAST_NAME
  123456   John           Smith
  234567   Ellen          Miller

so I did something like

  r = re.compile(r"(\S+)\s*")
  with open("data.txt") as f:
    headers = next(f)
    header_map = dict(
      (m.group(1), slice(m.start(), m.end()))
      for m in r.finditer(headers)
      )
    for row in f:
      empid = row[header_map["EMPID"]].rstrip()
      fname = row[header_map["FIRST_NAME"]].rstrip()
      lname = row[header_map["LAST_NAME"]].rstrip()
      process(empid, fname, lname)
      # or
      # data = dict(
      #   (field, row[slc])
      #   for field, slc in row.items()
      #   )
      # process(data)


Hope this gives you some helpful techniques.

-tkc





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


Thread

Re: Simplest way to locate a string in a column and get the value on the same row in another column Tim Chase <python.list@tim.thechases.com> - 2016-04-28 14:06 -0500

csiph-web