python 批量json转txt
时间: 2023-09-16 18:10:25 浏览: 87
可以使用Python内置的json库来实现json转换。以下是一个示例代码:
```python
import json
# 读取json文件
with open('data.json', 'r') as f:
data = json.load(f)
# 将json转换为字符串
result = json.dumps(data)
# 将结果写入txt文件
with open('result.txt', 'w') as f:
f.write(result)
```
以上代码将把名为"data.json"的JSON文件转换为文本文件"result.txt"。您只需将"data.json"替换为您的实际文件名即可。
相关问题
批量json转txt格式,X1,Y1图片中心坐标宽高 ,图片大小不同并归一化,python代码
以下是将批量JSON文件转换为TXT格式,并将坐标转换为图片中心坐标和宽高的Python代码示例:
```python
import json
import csv
import os
def json_to_txt(json_folder, txt_folder):
if not os.path.exists(txt_folder):
os.makedirs(txt_folder)
json_files = os.listdir(json_folder)
for json_file in json_files:
if not json_file.endswith('.json'):
continue
json_path = os.path.join(json_folder, json_file)
txt_file = json_file.replace('.json', '.txt')
txt_path = os.path.join(txt_folder, txt_file)
with open(json_path, 'r') as f:
data = json.load(f)
with open(txt_path, 'w', newline='') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(['X', 'Y', 'Width', 'Height'])
for obj in data['objects']:
x1 = obj['x1']
y1 = obj['y1']
x2 = obj['x2']
y2 = obj['y2']
# 计算中心坐标
width = x2 - x1
height = y2 - y1
x = (x1 + x2) / 2
y = (y1 + y2) / 2
# 归一化处理
x /= data['width']
y /= data['height']
width /= data['width']
height /= data['height']
writer.writerow([x, y, width, height])
# 使用示例
json_folder = 'input_folder'
txt_folder = 'output_folder'
json_to_txt(json_folder, txt_folder)
```
在上述代码中,我们首先创建一个输出文件夹(如果不存在)。然后,我们列出输入文件夹中的所有JSON文件。对于每个JSON文件,我们按照之前的逻辑进行处理,并将结果写入到对应的TXT文件中。请确保将实际的输入文件夹和输出文件夹路径替换为你自己的文件夹路径。
json批量转txt python
要将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文件。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)