如何使用多个对象循环通过JSON数据

发布于 2025-02-07 17:29:57 字数 555 浏览 2 评论 0原文

我的json文件data.json看起来

[
{"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
{"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]

我想以这种方式发生循环(让我们忽略远程变量)

for remotes,host,username in zip(remote , data["host"] ,data["username"]):

这是我遇到的错误

    for remotes,host,username in list(zip(remote , data["host"] ,data["username"])):
TypeError: list indices must be integers or slices, not str

My json file data.json looks like this

[
{"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
{"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]

I want the loop happen in this way only (lets ignore the remote variable)

for remotes,host,username in zip(remote , data["host"] ,data["username"]):

This is the error i am getting

    for remotes,host,username in list(zip(remote , data["host"] ,data["username"])):
TypeError: list indices must be integers or slices, not str

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吹梦到西洲 2025-02-14 17:29:57

您需要迭代数据以提取host用户名值,以便您可以zip它们到远程>远程列表:

data = [
 {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
 {"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]
hosts_users = [(d['host'], d['username']) for d in data]
remote = [1, 2]

for remote, (host, username) in zip(remote, hosts_users):
    print(remote, host, username)

输出:

1 192.168.0.25 server2
2 192.168.0.26 server3

You need to iterate the data to extract the host and username values so that you can zip them to the remote list:

data = [
 {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
 {"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]
hosts_users = [(d['host'], d['username']) for d in data]
remote = [1, 2]

for remote, (host, username) in zip(remote, hosts_users):
    print(remote, host, username)

Output:

1 192.168.0.25 server2
2 192.168.0.26 server3
清音悠歌 2025-02-14 17:29:57

如果您有JSON首先需要阅读,则可以将数据操纵为Python对象

import json

with open("data.json") as json_file:
    data = json.load(json_file)

for d in data:
    host = d['host']
    username = d['username']
    path = d['path']
    print(host, username, path)

if you have json file first you need to read and after that, you can manipulate that data as a python object

import json

with open("data.json") as json_file:
    data = json.load(json_file)

for d in data:
    host = d['host']
    username = d['username']
    path = d['path']
    print(host, username, path)
琉璃繁缕 2025-02-14 17:29:57

您可以使用 map zip 喜欢

# uncomment following code if data reside in json
# import json
# file = open('path_of_your_json')
# data = json.load(file)
data = [
    {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
    {"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]

for (host, username, path) in zip(*zip(*map(lambda x: x.values(), data))): 
   print(host, username, path)   
   # whatever you want

zip(*zip(*map(lambda x:x.values(),data)))该行将以线性方式提供数据

You can do by using map with zip like

# uncomment following code if data reside in json
# import json
# file = open('path_of_your_json')
# data = json.load(file)
data = [
    {"host" : "192.168.0.25", "username":"server2", "path":"/home/server/.ssh/01_id"},
    {"host" : "192.168.0.26", "username":"server3", "path":"/home/server/.ssh/01_id"}
]

for (host, username, path) in zip(*zip(*map(lambda x: x.values(), data))): 
   print(host, username, path)   
   # whatever you want

zip(*zip(*map(lambda x: x.values(), data))) this line will provide the data in linear way

勿忘心安 2025-02-14 17:29:57

由于您特别提到了使用Zip列Wise遍历数据,因此您可以做到这一点。

说json文件名是如此。json

在变量数据中加载JSON对象。

import json
f = open(r'C:\Users\YYName\Desktop\Temp\SO.json')
data = json.load(f)

现在,您可以使用zip和列遍地遍历这些值。将JSON数据加载到PANDAS数据框架中。

import pandas as pd
df = pd.DataFrame(data)
for host,username in zip(df["host"] ,df["username"]):
    print(host, username)

假设遥控器的长度与JSON中的行数相同。你现在可以做

for remotes,host,username in zip(remote , df["host"] ,df["username"]):
    print(remotes, host, username)

Since you mentioned specifically that you would like to iterate through the data using zip column wise, here is how you can do that.

Say the json file name is SO.json

Load the json object in the variable data.

import json
f = open(r'C:\Users\YYName\Desktop\Temp\SO.json')
data = json.load(f)

Now you can iterate through the values using zip and through columns. Load the json data in a pandas dataframe.

import pandas as pd
df = pd.DataFrame(data)
for host,username in zip(df["host"] ,df["username"]):
    print(host, username)

Assuming remote to be of same length as the number of rows in your json. You can now do

for remotes,host,username in zip(remote , df["host"] ,df["username"]):
    print(remotes, host, username)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文