如何使用多个对象循环通过JSON数据
我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要迭代数据以提取
host
和用户名
值,以便您可以zip
它们到远程>远程
列表:输出:
You need to iterate the data to extract the
host
andusername
values so that you canzip
them to theremote
list:Output:
如果您有
JSON
首先需要阅读,则可以将数据操纵为Python对象if you have
json
file first you need to read and after that, you can manipulate that data as a python object您可以使用 map 与 zip 喜欢
zip(*zip(*map(lambda x:x.values(),data)))该行将以线性方式提供数据
You can do by using map with zip like
zip(*zip(*map(lambda x: x.values(), data))) this line will provide the data in linear way
由于您特别提到了使用Zip列Wise遍历数据,因此您可以做到这一点。
说json文件名是如此。json
在变量数据中加载JSON对象。
现在,您可以使用zip和列遍地遍历这些值。将JSON数据加载到PANDAS数据框架中。
假设遥控器的长度与JSON中的行数相同。你现在可以做
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.
Now you can iterate through the values using zip and through columns. Load the json data in a pandas dataframe.
Assuming remote to be of same length as the number of rows in your json. You can now do