1
[{
    'original_block': '213.158.64.0/19',
    'transferred_blocks': '213.158.64.0/19',
    'from': 'REGISTER.IT S.P.A.',
    'to': 'REGISTER S.P.A.',
    'date': '01/07/2019',
    'transferType': 'MERGER_OR_ACQUISITION',
    }, {
    'original_block': '5.154.240.0/23',
    'transferred_blocks': '5.154.240.0/24',
    'from': 'NAV COMMUNICATIONS SRL',
    'to': 'uPress Inc',
    'date': '01/07/2019',
    'transferType': 'POLICY',
    }, {
    'original_block': '78.159.136.0/21',
    'transferred_blocks': '78.159.140.0/22',
    'from': 'Telecom Aset Ltd',
    'to': 'Aryaka Networks Inc.',
    'date': '01/07/2019',
    'transferType': 'POLICY',
    }, {
    'original_block': '81.88.48.0/20',
    'transferred_blocks': '81.88.48.0/20',
    'from': 'REGISTER.IT S.P.A.',
    'to': 'REGISTER S.P.A.',
    'date': '01/07/2019',
    'transferType': 'MERGER_OR_ACQUISITION',
    }]

I have a list of IP transfers information that I loaded from a JSON file. I want to extract some values from the JSON file (dictionary) but I keep getting 'list' object has no attribute 'values'.

import json

with open ('iplist.json','r') as t:

    d = json.load(t)

address = [item['original_block'] for d_ in d.values() for item in d_]

print(address)

I get

'list' object has no attribute 'values'

I expect to see

address = ['213.158.64.0/19', '5.154.240.0/23', '78.159.136.0/21', '81.88.48.0/20']
2
  • address = [item['original_block'] for item in d] Commented Jul 10, 2019 at 12:55
  • it worked, thanks. What was my error please? Commented Jul 10, 2019 at 12:58

1 Answer 1

1

d is a list of dictionaries, each of which has a original_block key.

just do this:

d = [{'original_block': '213.158.64.0/19', 'transferred_blocks': '213.158.64.0/19', 'from': 'REGISTER.IT S.P.A.', 'to': 'REGISTER S.P.A.', 'date': '01/07/2019', 'transferType': 'MERGER_OR_ACQUISITION'}, {'original_block': '5.154.240.0/23', 'transferred_blocks': '5.154.240.0/24', 'from': 'NAV COMMUNICATIONS SRL', 'to': 'uPress Inc', 'date': '01/07/2019', 'transferType': 'POLICY'}, 
{'original_block': '78.159.136.0/21', 'transferred_blocks': '78.159.140.0/22', 'from': 'Telecom Aset Ltd', 'to': 'Aryaka Networks Inc.', 'date': '01/07/2019', 'transferType': 'POLICY'}, 
{'original_block': '81.88.48.0/20', 'transferred_blocks': '81.88.48.0/20', 'from': 'REGISTER.IT S.P.A.', 'to': 'REGISTER S.P.A.', 'date': '01/07/2019', 'transferType': 'MERGER_OR_ACQUISITION'}]

address = [item['original_block'] for item in d]

print(address)

Output:

['213.158.64.0/19', '5.154.240.0/23', '78.159.136.0/21', '81.88.48.0/20']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.