如何在Python中创建一个循环,使用来自不同列表的两个参数来编写JSON文件?

huangapple go评论73阅读模式
英文:

How to create a loop to write json files with two arguments from difrent lists in python

问题

I would like to modify my code with a for loop for:

  • first to create a list of files to create fnames = []
  • secondly to create a list of json_dics = []

I currently have a separate variable for each file

file_xx = f'{root_path}/{output}/{file_name_xx}.{json_ext}'

and separate variable for each json_dics

json_dic_xx = [{"test": "test", "test1": "test1"}]

I use functions to write data to files

def save_to_json(json_dic, data_to_save):
    with open(data_to_save, 'w', encoding='utf_8') as json_file:
        json.dump(json_dic, json_file, ensure_ascii=False, indent=4, sort_keys=True)

save_to_json(json_dic_xx, file_xx)

I would like to create a for loop that, using the save_to_json() function, would take two arguments, one from the fnames = [], the other from the json_dics = [], and write the data to the appropriate files accordingly.

EDIT: my solution after making corrections by Rubén Pérez

output_file_names = ['filename01', 'filename02', 'filename03']

fnames = []
for fname in output_file_names:
    fnames.append(f'{root_path}/{output}/{fname}.{json_ext}')

json_dics = [[] for _ in range(len(fnames))]

for i in range(len(fnames)):
    save_to_json(json_dics[i], fnames[i])
英文:

I would like to modify my code with a for loop for:

  • first to create a list of files to create fnames = []
  • secondly to create a list of json_dics = []

I currently have a separate variable for each file

file_xx = f'{root_path}/{output}/{file_name_xx}.{json_ext}'

and separate variable for each json_dics

json_dic_xx = [{"test": "test", "test1" : "test1"}]

I use functions to write data to files

def save_to_json(json_dic, data_to_save):
    with open(data_to_save, 'w', encoding= utf_8) as json_file:
        json.dump(json_dic, json_file, ensure_ascii= False, indent=4, sort_keys= True)

save_to_json(json_dic_xx, file_xx)

I would like to create a for loop that, using the save_to_json() function, would take two arguments, one from the fnames = [], the other from the json_dics = [], and write the data to the appropriate files accordingly.

EDIT: my solution after making corrections by Rubén Pérez

output_file_names = ['filename01', `filename02', 'filename03']

fnames = []
for fname in output_file_names:
    fnames.append(f'{root_path}/{output}/{fname}.{json_ext}')

json_dics = [[] for _ in range(len(fnames))]


for i in range(len(fnames)):
    save_to_json(json_dics[i], fnames[i])

答案1

得分: 1

for i in range(xx):
    save_to_json(json_dics[i], fnames[i])

EDIT: You can also create a loop for the first lines:

Assume you have every file_name_xx in a list filenames = [].

fnames = []
for filename in filenames:
     fnames.append(f'{root_path}/{output}/{filename}.{json_ext}')

You can do the same with json_dic


<details>
<summary>英文:</summary>

for i in range(xx):
save_to_json(json_dics[i], fnames[i])


EDIT: You can also create a loop for the first lines:

Assume you have every `file_name_xx` in a list `filenames = []`.

fnames = []
for filename in filenames:
fnames.append(f'{root_path}/{output}/{filename}.{json_ext}')


You can do the same with json_dic


</details>



# 答案2
**得分**: 0

你可以使用zip()函数来同时迭代json_dics列表,这将有助于处理。以下是一个示例:

```python
json_dics = [json_dic_01, json_dic_02, json_dic_03, json_dic_04, json_dic_05, json_dic_06, json_dic_07]
fnames = [file_01, file_02, file_03, file_04, file_05, file_06, file_07]

for json_dic, fname in zip(json_dics, fnames):
    save_to_json(json_dic, fname)
英文:

You can use zip() function to iterate over both the json_dics list as it will help with that. Below is an example:

json_dics = [json_dic_01, json_dic_02, json_dic_03, json_dic_04, json_dic_05, json_dic_06, json_dic_07]
fnames = [file_01, file_02, file_03, file_04, file_05, file_06, file_07]

for json_dic, fname in zip(json_dics, fnames):
    save_to_json(json_dic, fname)

huangapple
  • 本文由 发表于 2023年3月3日 19:10:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定