Reputation: 759
I have a list which is in this certain format.
[u' ', u'Address :',u'Sadar Bazaar',u'new Delhi,India',u' ',u'Name :',u'Saun-Jean',u' ',u'Occupation :',u'Developer',u'Hacker',u' ']
I want to Insert Records into Database.
Here is my thought ,How to do it.
1) Take all items between two u' '
2) The second item u'Address'
defines the field of the Database and rest up-to next u' '
defines the data.
like
'Address :','Sadar Bazaar','new Delhi,India'
3)Repeat this procedure for all Items.
There may be other good ideas.
But i don't know how to do it in Python.Can someone help me do it??
Edit: Here is how i constructed the List:
for tr in browser.find_elements_by_xpath("//tbody//tbody//tr"):
tds=tr.find_elements_by_tag_name('td')
if tds:
data.append([td.text for td in tds])
Upvotes: 0
Views: 104
Reputation: 3454
Using your data as is:
l = [u' ', u'Address :',u'Sadar Bazaar',u'new Delhi,India',u' ',u'Name :',u'Saun-Jean',u' ',u'Occupation :',u'Developer',u'Hacker',u' ']
entries = {}
key = ''
for i in range(len(l)):
if l[i] == u' ' and i + 1 < len(l):
key = l[i + 1].replace(':', '').strip()
entries[key] = []
elif entries.has_key(l[i].replace(':', '').strip()):
continue
else:
# remove trailing space
if l[i] != u' ':
entries[key].append(l[i])
print entries
Outputs as a Dictionary:
{u'Occupation': [u'Developer', u'Hacker'], u'Name': [u'Saun-Jean'], u'Address': [u'Sadar Bazaar', u'new Delhi,India']}
Upvotes: 0
Reputation: 251166
lis=[u' ', u'Address :',u'Sadar Bazaar',u'new Delhi,India',u' ',u'Name :',u'Saun-Jean',u' ',u'Occupation :',u'Developer',u'Hacker',u' ']
strs=' '.join(str(x).strip() for x in lis if str(x).strip())
lis1=strs.split(':')
dic={}
for i,x in enumerate(lis1[:-1]):
if x.strip():
temp_lis=x.strip().split()
if i+1 <len(lis1)-1:
dic[temp_lis[-1]]=' '.join(lis1[i+1].split()[:-1])
else:
dic[temp_lis[-1]]=' '.join(lis1[i+1].split())
print dic
output:
{'Occupation': 'Developer Hacker', 'Name': 'Saun-Jean', 'Address': 'Sadar Bazaar new Delhi,India'}
Upvotes: 1
Reputation: 310257
This is much better done as a dictionary:
d={}
d['Address'] = ['sadar bazaar', ...]
d['Name'] = [ 'saun-jean', ... ]
...
Or perhaps as a list of dictionaries (or class instances):
[ {'Address' : 'sadar bazaar', 'Name': 'saun-jean'}, { ... } ]
To convert your list into a list of dictionaries like I have above, you can do the following:
from collections import defaultdict
d = defaultdict(list)
a = iter(yourlist)
key = None
for elem in a:
if elem == u' ':
key = next(a)
else:
d[key].append(elem)
Upvotes: 1
Reputation: 97
You have to construct dictionary at first place, that could be done by doing something like this(rough code):
data = {'Adress' : '',
'Name' : '',
'Occupation' : ''}
for item in tds:
data['Adress'] = item[0]
data['Name'] = item[1]
data['Occupation'] = item[2]
ofcourse this is in case that trs in tds are always at the same place. And then you can use the "data" dictionary to pull data by name
Upvotes: 0