I am using a module called Simple Salesforce to query data from my Salesforce database. The data comes back as an OrderedDict. How can I parse this into simple python lists.
Sample of the first two entries as it is returned when queried:
OrderedDict([
(u'totalSize', 418),
(u'done', True),
(u'records', [
OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/Case/11111')
])),
(u'Id', u'11111'),
(u'Subject', u'Case 1')
]),
OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/Case/2222222')
])),
(u'Id', u'2222222'),
(u'Subject', u'Case 2')
]),
#...
])
])
I not sure I have the term list correct, but I want it as a multi-dimensional table in the form of:
[('11111', 'Case 1'),('2222222', 'Case 2')]
Ultimately, I'd like to inner join this list with another list. Is this the best way to set up the data?
So the two lists that I would like to inner join would be :
List 1:
List1 = [('11111', 'Case 1'),('2222222', 'Case 2')] # [ID, Subject]
List 2:
List2 = [('11111', 'April'),('2222222', 'March'),('333333', 'January')] # [ID, Date]
Desired Output:
[('11111', 'Case 1','April'),('2222222', 'Case 2','March')]
What you have is a dict that contains, among other things, other dicts. Extrapolating from your desired output, what I believe you want is to convert this data structure into a list containing the Id
and Subject
of each element under the top-level records
key.
Now that we've defined the requirement, the solution presents itself easily: loop over the records
list and make tuples containing the desired properties. Let's say that top-level object is called data
. Then:
output = []
for record in data['records']: # Loop over all the records (each one an OrderedDict) in the list
subject = record['Subject'] # Extract the required information
identifier = record['Id']
output.append((identifier, subject)) # Add the extracted info to the output list
print(output) # Prints: [('11111', 'Case 1'), ('2222222', 'Case 2')]
Once you're comfortable with the basic idea, you can actually condense this loop into a list comprehension , which may be much faster to create if your input is large. The following line is equivalent to the loop above, but cleaner to read (for someone familiar with Python structures, anyway).
output = [(record['Id'], record['Subject']) for record in data['records']]
You can obtain the two dimensional table using a list comprehension :
from collections import OrderedDict
od = OrderedDict([
(u'totalSize', 418),
(u'done', True),
(u'records', [
OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/Case/11111')
])),
(u'Id', u'11111'),
(u'Subject', u'Case 1')
]),
OrderedDict([
(u'attributes', OrderedDict([
(u'type', u'Case'),
(u'url', u'/services/Case/2222222')
])),
(u'Id', u'2222222'),
(u'Subject', u'Case 2')
]),
#...
])
])
list1 = [(record['Id'], record['Subject']) for record in od['records']]
print list1 # -> [(u'11111', u'Case 1'), (u'2222222', u'Case 2')]
An "inner join" could be imitated with code something like this:
list2 = [('11111', 'April'), ('2222222', 'March'), ('333333', 'January')]
joined = [item1+item2[1:] for item1 in list1
for item2 in list2
if item1[0] == item2[0]]
print joined # -> [(u'11111', u'Case 1', 'April'),
# (u'2222222', u'Case 2', 'March')]
Note: The latter is somewhat inefficient, so you'd want to use more advanced processing techniques and/or data-structures to process large datasets quickly.
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.