Austin Johnson
Austin Johnson

Reputation: 747

Organizing data in Python Dictionaries

I'm puling some data from a webpage using selenium

table_body = browser.find_element_by_tag_name('tbody').text
print(table_body)

this give me back

'1 LA, California 3 bed room 845,600 \n1
2 OK, Oklahoma city 3 bed room 160,000 \n2
3 TX, Dallas 1 bed room 60,000' \n3

however, it's all one string with multiple line. Is there a way I can separate each individual character so I can append it to a dictionary. I've tried to .append it to a list and it returned [[...]],[[...]] and I tried to .update it a dictionary and I get an error saying ValueError: dictionary update sequence element #0 has length 3; 2 is required

UPDATE: I think my question was confusing the way I formatted it.

Upvotes: 0

Views: 763

Answers (3)

AkshayDandekar
AkshayDandekar

Reputation: 435

Assuming table_body is a string,

>>> table_body.split("|")

Edit: This should work then.

>>> for row in table_body.split("|"):
...    print row.split(',')

Edit 2: If there are no '|' symbols and only newlines.

>>> for row in table_body.split("\n"):
...    print row.split(',')

Upvotes: 2

Sidon
Sidon

Reputation: 1366

Consider var lst:

lst = 'Rank | Name | State | Position | cost | value | etc...'

If you make:

>>> table_body = lst.split("|")

Then print table_body:

>>>print (table_body)
['Rank ', ' Name ', ' State ', ' Position ', ' cost ', ' value ', ' 
  etc...']

If you try

>>> dict1 = dict(table_bodyt)

you will get an error:

dictionary update sequence element #0 has length 1; 2 is required

Why?

Because for to convert a list into a dict, each element of the list need be a sublist with two elements, one for key and other for value. For exemple:

>>> lst2 = [[n,table_body[n]] for n in range(len(table_body))]
>>> dict(lst2)
{0: 'Rank ',
 1: ' Name ',
 2: ' State ',
 3: ' Position ',
 4: ' cost ',
 5: ' value ',
 6: ' etc...'}

Upvotes: 0

JkShaw
JkShaw

Reputation: 1947

To get dictionary from string with default value None, for example:

>>> table_body = 'Rank | Name | State | Position | cost | value'

# List with stripped whitespaces
>>> [s.strip() for s in table_body.split('|')]
['Rank', 'Name', 'State', 'Position', 'cost', 'value']

# Dictionary from tab table_body
>>> dict([(s.strip(), None) for s in table_body.split('|')])
{'Name': None, 'value': None, 'State': None, 'cost': None, 'Rank': None, 'Position': None}

Upvotes: 1

Related Questions