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


Groups > comp.lang.python > #107825 > unrolled thread

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

Started byTim Chase <python.list@tim.thechases.com>
First post2016-04-28 14:06 -0500
Last post2016-04-28 14:06 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  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

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

FromTim Chase <python.list@tim.thechases.com>
Date2016-04-28 14:06 -0500
SubjectRe: Simplest way to locate a string in a column and get the value on the same row in another column
Message-ID<mailman.210.1461870637.32212.python-list@python.org>
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





[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web