上面是一个json文件,下面是代码,在代码中已经解释了大部分问题,如下疑问,欢迎留言。
import json
from os import listdir
import os
path = 'C:/Users/86187/Desktop/钓鱼项目/数据文件/终版整合数据/json/'
filelist = listdir(path) #批量获取json文件路径
fileIndex = []
# 文件名读入时并非按照我们常识中的按照文件名字顺序读入,
# 例如:1.json,2.json,3.json;程序可能会按 3,1,2 的顺序读入,
# 这对我们后面批量处理造成很大的不便,所以读入文件名后,
# 我们要手动地对文件名进行一次排序
# 以下就是排序操作
for i in range(0, len(filelist)):
index = filelist[i].split(".")[0]
fileIndex.append(int(index))
# new_filelist =[]
for j in range(1, len(fileIndex)):
for k in range(0, len(fileIndex) - 1):
if fileIndex[k] > fileIndex[k + 1]:
preIndex = fileIndex[k]
preFile = filelist[k]
fileIndex[k] = fileIndex[k + 1]
filelist[k] = filelist[k + 1]
fileIndex[k + 1] = preIndex
filelist[k + 1] = preFile
# 完成排序后,开始按照文件名顺序读取文件内容信息
data = [] # 记录每个文件最终信息的列表
labelpath = r'C:/Users/86187/Desktop/钓鱼项目/数据文件/终版整合数据/json/'
# labelpath 是我们遍历的json文件 跟上面定义的path一样
for file in filelist:
with open(labelpath + file, 'r') as txt:
lines = txt.readlines()
eachdata = [] # 记录单个文件信息的列表
# eachdata2=[]
eachdata.append(file) # fileName
# eachdata2.append(file)
for each in range(0, len(lines)): #读取json的每一行
#下面的each[]不是很好理解 , 实际上加几就是第几行 3 4 7 8 实际上是取的四个坐标
word = lines[each].split('"')
for i in range(0, len(word)):
if word[i] == 'person_fbox': #定位到labelh行
lines[each+3]=lines[each + 3].strip() #把json文件中坐标前的空格删除
# eachdata.append(json.load(txt)["points"])
eachdata.append((" "+lines[each + 3].split(',')[0]))
lines[each + 4] = lines[each + 4].replace("\n", " ") #将第二个坐标后的换行替换掉 否则会换行
lines[each + 4] = lines[each + 4].strip() #把json文件中坐标前的空格删除
eachdata.append((" "+lines[each + 4].split(',')[0]))
lines[each + 7] = lines[each + 7].strip() #把json文件中坐标前的空格删除
eachdata.append((" "+lines[each + 7].split(',')[0]))
lines[each + 8]=lines[each + 8].replace("\n"," ")#将第四个坐标后的换行替换掉 否则会换行
lines[each + 8] = lines[each + 8].strip() #把json文件中坐标前的空格删除
eachdata.append(
(" "+lines[each + 8].split(',')[0]) ) # (xmin,ymin,xmax,ymax,'person')
# eachdata.append('person')
elif word[i] == 'fish':
# eachdata.append(json.load(txt)["points"])
lines[each + 3] = lines[each + 3].strip()
# eachdata.append(json.load(txt)["points"])
eachdata.append((" " + lines[each + 3].split(',')[0]))
lines[each + 4] = lines[each + 4].replace("\n", " ") # 将第二个坐标后的换行替换掉 否则会换行
lines[each + 4] = lines[each + 4].strip()
eachdata.append((" " + lines[each + 4].split(',')[0]))
lines[each + 7] = lines[each + 7].strip()
eachdata.append((" " + lines[each + 7].split(',')[0]))
lines[each + 8] = lines[each + 8].replace("\n", " ") # 将第四个坐标后的换行替换掉 否则会换行
lines[each + 8] = lines[each + 8].strip()
eachdata.append(
(" " + lines[each + 8].split(',')[0])) # (xmin,ymin,xmax,ymax,'fish')
# eachdata.append('fish')
txt.close()
data.append(eachdata)
print(data)
# print(data)
# 记录完成后,开始将数据写入txt文件
with open(r'C:\Users\86187\Desktop\data.txt', 'w') as txt: #写入的txt文件
for eachdata in data:
line = eachdata[0] + ''
i = 0
for i in range(1, len(eachdata)):
if i % 5 == 0 and i > 1 and i != len(eachdata) - 1:
s = str(eachdata[i])
line = line + s
elif i == len(eachdata) - 1:
line = line + str(eachdata[i])
else:
s = str(eachdata[i])
line = line + s
lines = line.replace('json', 'jpg')+"\n" #控制每一个路径后换行
txt.writelines(lines)
txt.close()
print('finish')