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

Tim Chase python.list at tim.thechases.com
Thu Apr 28 15:06:51 EDT 2016


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









More information about the Python-list mailing list