I have this code:
while i<len(line):
if re.findall(pattern, line[i]):
k,v = line[i].split('=')
print k
token = dict(k=v)
print token
break
and the result I'm getting is :
ptk
{'k': 'ptk_first'}
how to make this few lines of code nicer and dictionary that will look like this:
{'ptk': 'ptk_first'}
for line in lines:
if re.match(pattern, line):
k,v = line.split('=')
token = {k:v}
print token
Something like this:
lines="""\
key1=data on the rest of line 1
key2=data on the rest of line 2
key3=data on line 3"""
d={}
for line in lines.splitlines():
k,v=line.split('=')
d[k]=v
print d
In [112]: line="ptk=ptk_first"
In [113]: dict([line.split("=")])
Out[113]: {'ptk': 'ptk_first'}
for your code:
for line in lines:
if re.findall(pattern, line):
token = dict([line.split("=")])
print token
with regex you can try this:
>>> import re
>>> lines="""
... ptk=ptk_first
... ptk1=ptk_second
... """
>>> dict(re.findall('(\w+)=(\w+)',lines,re.M))
{'ptk1': 'ptk_second', 'ptk': 'ptk_first'}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.