Im trying to convert a list with colons separated elements into a dictionary, but im getting error using dict()
.
Error example:
ValueError: dictionary update sequence element #0 has length 7; 2 is required
This is an example of the list Im trying to convert:
['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS', 'descr:APNICandCloudflareDNSResolverproject', 'descr:RoutedgloballybyAS13335/Cloudflare', 'descr:ResearchprefixforAPNICLabs', 'country:AU', 'org:ORG-ARAD1-AP', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'abuse-c:AA1412-AP', 'status:ASSIGNEDPORTABLE', 'remarks:---------------', 'remarks:AllCloudflareabusereportingcanbedonevia', 'remarks:<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', 'remarks:---------------', 'mnt-by:APNIC-HM', 'mnt-routes:MAINT-AU-APNIC-GM85-AP', 'mnt-irt:IRT-APNICRANDNET-AU', 'last-modified:2020-07-15T13:10:57Z', 'source:APNIC', '', 'irt:IRT-APNICRANDNET-AU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'auth:#Filtered', 'remarks:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2021-03-09T01:10:21Z', 'source:APNIC', '', 'organisation:ORG-ARAD1-AP', 'org-name:APNICResearchandDevelopment', 'country:AU', 'address:6CordeliaSt', 'phone: 61-7-38583100', 'fax-no: 61-7-38583199', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-ref:APNIC-HM', 'mnt-by:APNIC-HM', 'last-modified:2017-10-11T01:28:39Z', 'source:APNIC', '', 'role:ABUSEAPNICRANDNETAU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:ZZ', 'phone: 000000000', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'nic-hdl:AA1412-AP', 'remarks:GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-by:APNIC-ABUSE', 'last-modified:2021-03-09T01:10:22Z', 'source:APNIC', '', 'role:APNICRESEARCH', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:AU', 'phone: 61-7-3858-3188', 'fax-no: 61-7-3858-3199', 'e-mail:<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'nic-hdl:AR302-AP', 'tech-c:AH256-AP', 'admin-c:AH256-AP', 'mnt-by:MAINT-APNIC-AP', 'last-modified:2018-04-04T04:26:04Z', 'source:APNIC', '', '', '', 'route:1.1.1.0/24', 'origin:AS13335', 'descr:APNICResearchandDevelopment', '6CordeliaSt', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2018-03-16T16:58:06Z', 'source:APNIC', '']
I would like to get something like:
{"inetnum":"1.1.1.0-1.1.1.255", "netname":"APNIC-LABS"}
What can i do?
CodePudding user response:
Use a regular for
loop for readability. Use str.split
to split on :
into a maximum of 2 elements. Skip strings where no :
is found.
lst = ['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS', 'descr:APNICandCloudflareDNSResolverproject', 'descr:RoutedgloballybyAS13335/Cloudflare', 'descr:ResearchprefixforAPNICLabs', 'country:AU', 'org:ORG-ARAD1-AP', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'abuse-c:AA1412-AP', 'status:ASSIGNEDPORTABLE', 'remarks:---------------', 'remarks:AllCloudflareabusereportingcanbedonevia', 'remarks:<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', 'remarks:---------------', 'mnt-by:APNIC-HM', 'mnt-routes:MAINT-AU-APNIC-GM85-AP', 'mnt-irt:IRT-APNICRANDNET-AU', 'last-modified:2020-07-15T13:10:57Z', 'source:APNIC', '', 'irt:IRT-APNICRANDNET-AU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'auth:#Filtered', 'remarks:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2021-03-09T01:10:21Z', 'source:APNIC', '', 'organisation:ORG-ARAD1-AP', 'org-name:APNICResearchandDevelopment', 'country:AU', 'address:6CordeliaSt', 'phone: 61-7-38583100', 'fax-no: 61-7-38583199', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-ref:APNIC-HM', 'mnt-by:APNIC-HM', 'last-modified:2017-10-11T01:28:39Z', 'source:APNIC', '', 'role:ABUSEAPNICRANDNETAU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:ZZ', 'phone: 000000000', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'nic-hdl:AA1412-AP', 'remarks:GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-by:APNIC-ABUSE', 'last-modified:2021-03-09T01:10:22Z', 'source:APNIC', '', 'role:APNICRESEARCH', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:AU', 'phone: 61-7-3858-3188', 'fax-no: 61-7-3858-3199', 'e-mail:<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'nic-hdl:AR302-AP', 'tech-c:AH256-AP', 'admin-c:AH256-AP', 'mnt-by:MAINT-APNIC-AP', 'last-modified:2018-04-04T04:26:04Z', 'source:APNIC', '', '', '', 'route:1.1.1.0/24', 'origin:AS13335', 'descr:APNICResearchandDevelopment', '6CordeliaSt', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2018-03-16T16:58:06Z', 'source:APNIC', '']
dct = {}
for s in lst:
tup = s.split(':', maxsplit=1)
if len(tup) < 2:
continue
else:
dct[tup[0]] = tup[1]
print(dct)
# {'inetnum': '1.1.1.0-1.1.1.255', 'netname': 'APNIC-LABS', 'descr': 'APNICResearchandDevelopment', 'country': 'AU', 'org': 'ORG-ARAD1-AP', 'admin-c': 'AH256-AP', 'tech-c': 'AH256-AP', 'abuse-c': 'AA1412-AP', 'status': 'ASSIGNEDPORTABLE', 'remarks': 'GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'mnt-by': 'MAINT-AU-APNIC-GM85-AP', 'mnt-routes': 'MAINT-AU-APNIC-GM85-AP', 'mnt-irt': 'IRT-APNICRANDNET-AU', 'last-modified': '2018-03-16T16:58:06Z', 'source': 'APNIC', 'irt': 'IRT-APNICRANDNET-AU', 'address': 'Australia', 'e-mail': '<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox': '<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'auth': '#Filtered', 'organisation': 'ORG-ARAD1-AP', 'org-name': 'APNICResearchandDevelopment', 'phone': ' 61-7-3858-3188', 'fax-no': ' 61-7-3858-3199', 'mnt-ref': 'APNIC-HM', 'role': 'APNICRESEARCH', 'nic-hdl': 'AR302-AP', 'route': '1.1.1.0/24', 'origin': 'AS13335'}
CodePudding user response:
If you just want to ignore the list elements that don't follow the key:value
format, filter to those that only contain one colon:
>>> data = ['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS', 'descr:APNICandCloudflareDNSResolverproject', 'descr:RoutedgloballybyAS13335/Cloudflare', 'descr:ResearchprefixforAPNICLabs', 'country:AU', 'org:ORG-ARAD1-AP', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'abuse-c:AA1412-AP', 'status:ASSIGNEDPORTABLE', 'remarks:---------------', 'remarks:AllCloudflareabusereportingcanbedonevia', 'remarks:<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', 'remarks:---------------', 'mnt-by:APNIC-HM', 'mnt-routes:MAINT-AU-APNIC-GM85-AP', 'mnt-irt:IRT-APNICRANDNET-AU', 'last-modified:2020-07-15T13:10:57Z', 'source:APNIC', '', 'irt:IRT-APNICRANDNET-AU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'auth:#Filtered', 'remarks:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2021-03-09T01:10:21Z', 'source:APNIC', '', 'organisation:ORG-ARAD1-AP', 'org-name:APNICResearchandDevelopment', 'country:AU', 'address:6CordeliaSt', 'phone: 61-7-38583100', 'fax-no: 61-7-38583199', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-ref:APNIC-HM', 'mnt-by:APNIC-HM', 'last-modified:2017-10-11T01:28:39Z', 'source:APNIC', '', 'role:ABUSEAPNICRANDNETAU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:ZZ', 'phone: 000000000', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'nic-hdl:AA1412-AP', 'remarks:GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-by:APNIC-ABUSE', 'last-modified:2021-03-09T01:10:22Z', 'source:APNIC', '', 'role:APNICRESEARCH', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:AU', 'phone: 61-7-3858-3188', 'fax-no: 61-7-3858-3199', 'e-mail:<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'nic-hdl:AR302-AP', 'tech-c:AH256-AP', 'admin-c:AH256-AP', 'mnt-by:MAINT-APNIC-AP', 'last-modified:2018-04-04T04:26:04Z', 'source:APNIC', '', '', '', 'route:1.1.1.0/24', 'origin:AS13335', 'descr:APNICResearchandDevelopment', '6CordeliaSt', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2018-03-16T16:58:06Z', 'source:APNIC', '']
>>> dict(i.split(":") for i in data if i.count(":") == 1)
{'inetnum': '1.1.1.0-1.1.1.255', 'netname': 'APNIC-LABS', 'descr': 'APNICResearchandDevelopment', 'country': 'AU', 'org': 'ORG-ARAD1-AP', 'admin-c': 'AH256-AP', 'tech-c': 'AH256-AP', 'abuse-c': 'AA1412-AP', 'status': 'ASSIGNEDPORTABLE', 'remarks': 'GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'mnt-by': 'MAINT-AU-APNIC-GM85-AP', 'mnt-routes': 'MAINT-AU-APNIC-GM85-AP', 'mnt-irt': 'IRT-APNICRANDNET-AU', 'source': 'APNIC', 'irt': 'IRT-APNICRANDNET-AU', 'address': 'Australia', 'e-mail': '<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox': '<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'auth': '#Filtered', 'organisation': 'ORG-ARAD1-AP', 'org-name': 'APNICResearchandDevelopment', 'phone': ' 61-7-3858-3188', 'fax-no': ' 61-7-3858-3199', 'mnt-ref': 'APNIC-HM', 'role': 'APNICRESEARCH', 'nic-hdl': 'AR302-AP', 'route': '1.1.1.0/24', 'origin': 'AS13335'}
Without that if i.count(":") == 1
filter, you get errors on some of the strings because some of them are empty (hence they have length 0 when split) and at least one has three :
s (hence it has length 4).
Note that this solution simply ignores those entries rather than trying to make sense of them or raise an error. If you wanted to do something else with the 3-:
entries (like split them into two key-value pairs, or allow for keys and/or values to contain :
characters) you'd need to settle on an approach and modify the way that the key:value sequence is built.
CodePudding user response:
Using a generator as the input to a dict() constructor has been shown to good effect by @Samwise
Here's a simple loop that might be easier to understand:
list_ = ['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS', 'descr:APNICandCloudflareDNSResolverproject', 'descr:RoutedgloballybyAS13335/Cloudflare', 'descr:ResearchprefixforAPNICLabs', 'country:AU', 'org:ORG-ARAD1-AP', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'abuse-c:AA1412-AP', 'status:ASSIGNEDPORTABLE', 'remarks:---------------', 'remarks:AllCloudflareabusereportingcanbedonevia', 'remarks:<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', 'remarks:---------------', 'mnt-by:APNIC-HM', 'mnt-routes:MAINT-AU-APNIC-GM85-AP', 'mnt-irt:IRT-APNICRANDNET-AU', 'last-modified:2020-07-15T13:10:57Z', 'source:APNIC', '', 'irt:IRT-APNICRANDNET-AU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'auth:#Filtered', 'remarks:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2021-03-09T01:10:21Z', 'source:APNIC', '', 'organisation:ORG-ARAD1-AP', 'org-name:APNICResearchandDevelopment', 'country:AU', 'address:6CordeliaSt', 'phone: 61-7-38583100', 'fax-no: 61-7-38583199', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-ref:APNIC-HM', 'mnt-by:APNIC-HM', 'last-modified:2017-10-11T01:28:39Z', 'source:APNIC', '', 'role:ABUSEAPNICRANDNETAU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:ZZ', 'phone: 000000000', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'nic-hdl:AA1412-AP', 'remarks:GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-by:APNIC-ABUSE', 'last-modified:2021-03-09T01:10:22Z', 'source:APNIC', '', 'role:APNICRESEARCH', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:AU', 'phone: 61-7-3858-3188', 'fax-no: 61-7-3858-3199', 'e-mail:<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'nic-hdl:AR302-AP', 'tech-c:AH256-AP', 'admin-c:AH256-AP', 'mnt-by:MAINT-APNIC-AP', 'last-modified:2018-04-04T04:26:04Z', 'source:APNIC', '', '', '', 'route:1.1.1.0/24', 'origin:AS13335', 'descr:APNICResearchandDevelopment', '6CordeliaSt', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2018-03-16T16:58:06Z', 'source:APNIC', '']
dict_ = {}
for e in list_:
k, *v = e.split(':', 1)
if v:
dict_[k] = v[0]
CodePudding user response:
Use the split
method on :
to separate the strings in your list into key-value pairs for the dict.
res = {}
data = ['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS', 'descr:APNICandCloudflareDNSResolverproject', 'descr:RoutedgloballybyAS13335/Cloudflare', 'descr:ResearchprefixforAPNICLabs', 'country:AU', 'org:ORG-ARAD1-AP', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'abuse-c:AA1412-AP', 'status:ASSIGNEDPORTABLE', 'remarks:---------------', 'remarks:AllCloudflareabusereportingcanbedonevia', 'remarks:<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', 'remarks:---------------', 'mnt-by:APNIC-HM', 'mnt-routes:MAINT-AU-APNIC-GM85-AP', 'mnt-irt:IRT-APNICRANDNET-AU', 'last-modified:2020-07-15T13:10:57Z', 'source:APNIC', '', 'irt:IRT-APNICRANDNET-AU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'auth:#Filtered', 'remarks:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2021-03-09T01:10:21Z', 'source:APNIC', '', 'organisation:ORG-ARAD1-AP', 'org-name:APNICResearchandDevelopment', 'country:AU', 'address:6CordeliaSt', 'phone: 61-7-38583100', 'fax-no: 61-7-38583199', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-ref:APNIC-HM', 'mnt-by:APNIC-HM', 'last-modified:2017-10-11T01:28:39Z', 'source:APNIC', '', 'role:ABUSEAPNICRANDNETAU', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:ZZ', 'phone: 000000000', 'e-mail:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'admin-c:AR302-AP', 'tech-c:AR302-AP', 'nic-hdl:AA1412-AP', 'remarks:GeneratedfromirtobjectIRT-APNICRANDNET-AU', 'abuse-mailbox:<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.net', 'mnt-by:APNIC-ABUSE', 'last-modified:2021-03-09T01:10:22Z', 'source:APNIC', '', 'role:APNICRESEARCH', 'address:POBox3646', 'address:SouthBrisbane,QLD4101', 'address:Australia', 'country:AU', 'phone: 61-7-3858-3188', 'fax-no: 61-7-3858-3199', 'e-mail:<imgsrc="/eimg/a/4a/a4a58e14449e98257260341784a655af43ebbb08.png"alt="email"loading="lazy">@apnic.net', 'nic-hdl:AR302-AP', 'tech-c:AH256-AP', 'admin-c:AH256-AP', 'mnt-by:MAINT-APNIC-AP', 'last-modified:2018-04-04T04:26:04Z', 'source:APNIC', '', '', '', 'route:1.1.1.0/24', 'origin:AS13335', 'descr:APNICResearchandDevelopment', '6CordeliaSt', 'mnt-by:MAINT-AU-APNIC-GM85-AP', 'last-modified:2018-03-16T16:58:06Z', 'source:APNIC', '']
for entry in data:
try:
k, v = entry.split(':')
res[k] = v
except ValueError:
pass
print(res)
Of note, you have one entry '6CordeliaSt'
that is neither separated by a colon, nor a blank string. Tweaking the code further would depend on whether or not you wanted to keep or ignore this entry (the above code will ignore it).
I also noticed that you have several entries that start with 'remarks'
, so this code above will only maintain the last one. A more complex solution would be to keep a list as the value of each dictionary key and append to it, like so
for entry in data:
try:
k, v = entry.split(':')
if k in res.keys():
res[k].append(v)
else:
res[k] = [v]
except ValueError:
pass
print(res)
This will still ignore strings that aren't split on a colon like 6CordeliaSt, but it will keep all values together that start with the same key, like remarks
, but your "simpler" entries will need to be accessed via the list
{
"inetnum": ["1.1.1.0-1.1.1.255"],
"netname": ["APNIC-LABS"],
"remarks": ['---------------', 'AllCloudflareabusereportingcanbedonevia', '<imgsrc="/eimg/f/5b/f5bbf78a66c792df55d04ee6bce9698d5c8accaf.png"alt="email"loading="lazy">@cloudflare.com', '---------------', '<imgsrc="/eimg/b/11/b116938aa3180504ab727885fc8426ee57dbb10b.png"alt="email"loading="lazy">@apnic.netwasvalidatedon2021-02-09', 'GeneratedfromirtobjectIRT-APNICRANDNET-AU']
}
CodePudding user response:
for exemple you can try this code plz my bro
list =['inetnum:1.1.1.0-1.1.1.255', 'netname:APNIC-LABS','']
d = {}
for x in list :
if ':' in x :
d[x.split(":", 1)[0]] = x.split(":", 1)[1]
print(d)