json批量转txt python
时间: 2023-11-11 16:01:27 浏览: 65
要将json批量转换为txt文件,可以使用Python来实现。首先,需要使用Python内置的json模块来读取json文件,然后将其转换为txt格式并写入到txt文件中。
首先,需要引入json模块:
```python
import json
```
然后,读取json文件并将其转换为txt格式:
```python
def json_to_txt(input_file, output_file):
with open(input_file, 'r', encoding='utf-8') as f:
data = json.load(f)
with open(output_file, 'w', encoding='utf-8') as f:
for item in data:
f.write(str(item) + '\n')
```
以上代码将json文件中的数据读取出来并逐行写入到txt文件中。在函数中,input_file是输入的json文件名,output_file是输出的txt文件名。
最后,调用该函数来批量转换json文件:
```python
file_list = ['file1.json', 'file2.json', 'file3.json']
for file in file_list:
json_to_txt(file, file.replace('.json', '.txt'))
```
以上代码将会批量转换file1.json、file2.json、file3.json并分别输出为file1.txt、file2.txt、file3.txt。
通过以上方法,可以很方便地使用Python将json文件批量转换为txt文件。