使用循环创建带有两个列表的多个JSON文件

发布于 2025-01-23 23:20:08 字数 455 浏览 0 评论 0原文

我想问如何用两个列表制作多个JSON文件? 假设我的输入是:

animal_list = ["cat", "dog", "bird", "fish", "chicken", "tiger"]
name_file = ["a", "b", "c", "d", "e", "f"]

我想要的输出是:

a.json -> "cat"
b.json -> "dog"
c.json -> "bird"
d.json -> "fish"
e.json -> "chicken"
f.json -> "tiger"

因此,文件a.json包含“ cat”,文件 b.json 包含<代码>“狗” 等。

I want to ask how to make multiple JSON files with two lists?
let's say my input is:

animal_list = ["cat", "dog", "bird", "fish", "chicken", "tiger"]
name_file = ["a", "b", "c", "d", "e", "f"]

and output that I want is:

a.json -> "cat"
b.json -> "dog"
c.json -> "bird"
d.json -> "fish"
e.json -> "chicken"
f.json -> "tiger"

so file a.json contains "cat", file b.json contains "dog" etc.

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

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

发布评论

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

评论(2

摇划花蜜的午后 2025-01-30 23:20:09

尝试一下:

import json

lst = ["cat", "dog", "bird"]
file_names = ["a", "b", "c"]

for file_name, content in zip(file_names, lst):
    with open(f"{file_name}.json", mode="w") as f:
        json.dump(content, f)

基本上,您是在缩放两个列表,然后在每次迭代中,您可以在字符串格式的帮助下打开一个新文件(您需要构建文件的名称),然后用json.dump 方法。

也不要为变量使用内置名称。我故意将列表重命名为lst

Try this:

import json

lst = ["cat", "dog", "bird"]
file_names = ["a", "b", "c"]

for file_name, content in zip(file_names, lst):
    with open(f"{file_name}.json", mode="w") as f:
        json.dump(content, f)

Basically you are zipping your two lists, then in each iteration you open a new file with the help of string formatting(you need to build the file's name) then you dump your objects with json.dump method.

Also do not use built-in names for your variable. I intentionally renamed list to lst.

洛阳烟雨空心柳 2025-01-30 23:20:08

您正在寻找zip()函数。它允许您同时迭代多个列表的元素。

从那里,您可以使用F-string打开每个文件作为.json文件来创建文件名,然后写出内容。

要写入JSON格式,Python具有内置的JSON软件包。这将确保您要保存的任何Python对象都被格式化为适当的JSON。我们将使用json.dump函数,该函数将您的内容带入文件对象。

import json

animal_list = ["cat", "dog", "bird", "fish", "chicken", "tiger"]
name_file = ["a", "b", "c", "d", "e", "f"]

for fname, animal in zip(name_file, animal_list):
    with open(f'{fname}.json', 'w') as fp:
        json.dump(animal, fp)

You are looking for the zip() function. It allows you to iterate over the elements of multiple lists at the same time.

From there you can open each file as a .json file using an f-string to create the file name, and then write out the contents.

To write out to the JSON format, Python has the built-in json package. This will ensure whatever Python object you want to save is formatted as proper JSON. We will use the json.dump function, which takes your content and writes it to a file object.

import json

animal_list = ["cat", "dog", "bird", "fish", "chicken", "tiger"]
name_file = ["a", "b", "c", "d", "e", "f"]

for fname, animal in zip(name_file, animal_list):
    with open(f'{fname}.json', 'w') as fp:
        json.dump(animal, fp)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文