Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Tim Chase 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: 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: 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-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 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 =C2=A0a2 =C2=A0b3 =C2=A0c > 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 =3D "1 a2 b3 c" s2 =3D "abcdefghijklmn" import re r =3D re.compile(r"a2\s*") # define the pattern you want m =3D r.search(s1) if m: # if our pattern was in the header # take the corresponding slice out of s2 result =3D 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 =3D re.compile(r"(\S+)\s*") with open("data.txt") as f: headers =3D next(f) header_map =3D dict( (m.group(1), slice(m.start(), m.end())) for m in r.finditer(headers) ) for row in f: empid =3D row[header_map["EMPID"]].rstrip() fname =3D row[header_map["FIRST_NAME"]].rstrip() lname =3D row[header_map["LAST_NAME"]].rstrip() process(empid, fname, lname) # or # data =3D dict( # (field, row[slc]) # for field, slc in row.items() # ) # process(data) Hope this gives you some helpful techniques. -tkc