Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #107825
| 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 | 2016-04-28 14:06 -0500 |
| 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> |
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
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