i have file on s3 that contains
[{
'address': 'Bramalea, L6T 0E2'
'type': 'home'
}, {
'address': 'A, 46 Peel Drive, ASDF23'
'type': 'office'
}
}]
i just wanted to read addresses that has type office , can any body suggest me how i can iterate this data ? because its just a string so far i am able to read this data
conn = S3Connection(AWS_KEY, AWS_SECRET)
bucket = conn.get_bucket(BUCKET_NAME)
for key in bucket.list(DIR_Name):
data = key.get_contents_as_string()
print data
i also tried reading data using json module
data = key.get_contents_as_string()
print json.loads(data)
its raising
print json.loads(data)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 2 column 5 (char 8)
It is not necessary to use an other module. As this is a list of dictionaries, it is easy to keep only elements with 'type'
that are 'office'
:
my_list = [{
'address': 'aa, 46 Peel Centre Drive, A342',
'type': 'home',
}, {
'address': 'a, 46 Peel Centre Drive, AS32',
'type': 'office',
}, {
'address': 'b, 46 Peel Centre Drive, SD22',
'type': 'home',
}, {
'address': 'c, 46 Peel Centre Drive, SD22',
'type': 'home',
}, {
'address': 'd, 46 Peel Centre Drive, SSD222',
'type': 'office',
}]
addresses = [elem['address'] for elem in my_list if elem['type'] == 'office']
print addresses
I was incorrect in my comment to your post. Do not use the json
module to parse this file because this is not valid json. Valid json only uses double quotes around its strings - not single quotes. Instead, this looks like basic python literal structures (read: basic python data types). There is a method to safely do this with potentially untrusted sources. ast.literal_eval
should be able to help you here.
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.