Understanding Python 3 JSON's In-Dict List Operation_2

Asked 2 months ago, Updated 2 months ago, 1 views

This is the person who asked me a question on here.I am writing to ask you to tell me the solution again.Thank you for your cooperation.

jsonFile.json

{
"Site Name 1": ["URL 11", "URL 12", "URL 13", ... "URL 1n",
"Site Name 2": ["URL21", "URL22", "URL23", ... "URL2n",
"Site Name 3": ["URL31", "URL32", "URL33", ... "URL3n",
...
"Site Name n": ["URLn1", "URLn2", "URLn3", ... "URLnn" ]
}

There are multiple site names in the key portion, each with a different URL.

import json

f=open("anidora.json")
datas=json.load(f)

keys=datas.keys()
values=datas.values()

d = [ ]
forking keys:
    row = [ ] 
    for vin datas [k]: 
        row.append([v,k])
    d.append(row)

d=list(zip(*d))

Forrind:
    For vinr:
        print('<li><a href=\"{0}\"target=\"_blank\">{1}</a>/li>'.format(v[0], v[1]))

#<li><a href="URL11" target="_blank">Site 1</a>/li>
#<li><a href="URL21" target="_blank">Site 2</a>/li>
#<li><a href="URL31" target="_blank">Site 3</a>/li>
#<li><a href="URL12" target="_blank">Site 1</a>/li>
#<li><a href="URL22" target="_blank">Site 2</a>/li>
#<li><a href="URL32" target="_blank">Site 3</a>/li>
#<li><a href="URL13" target="_blank">Site 1</a>/li>
#<li><a href="URL23" target="_blank">Site 2</a>/li>
#<li><a href="URL33" target="_blank">Site 3</a>/li>

I was satisfied with this result, but if the number of array values is large, the output was only halfway through.
For example: key: site name 18, value: 48 URLs, up to 23rd URL
For key: site name 18, value: 28 URLs, only the eighth URL was displayed.

d.append(row)

d=list(zip(*d))

There seems to be a change in the number of arrays during this time, but I don't know the cause.
Please tell me how to print all the sites and URLs even if they increase.
I am sorry to ask you again, but I would appreciate it if you could let me know.

python json python3

2022-09-30 14:02

1 Answers

How do you like this?

#!/usr/bin/env python3
# -*-coding:utf-8-*-
import json

with open("jsonFile.json") asf:
    data=json.load(f)

sites=sorted(data)
while any(data.values()):
    for site in sites:
        if not data [site]:
            continue
        url=data[site].pop(0)
        print(""<li><a href="{}" target="_blank">{}</a><"".".format(site, url))


2022-09-30 14:02

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.