如何在python中修改Yolo .txt文件的值



我想修改一个包含。txt文件的文件夹

文本文件看起来像这样:

3 0.695312 0.523958 0.068750 0.052083
3 0.846875 0.757292 0.071875 0.031250
3 0.830469 0.719792 0.067187 0.035417

我的想法是把所有的。txt文件和改变内联第一个数字。

输出示例:

2 0.695312 0.523958 0.068750 0.052083
2 0.846875 0.757292 0.071875 0.031250
2 0.830469 0.719792 0.067187 0.035417

你能帮我吗?

我认为这段代码应该删除。让我知道这是你想要的。

import os
files = []
# Add the path of txt folder
for i in os.listdir("C:data"):
if i.endswith('.txt'):
files.append(i)
for item in files:
# define an empty list
file_data = []
# open file and read the content in a list
with open(item, 'r') as myfile:
for line in myfile:
# remove linebreak which is the last character of the string
currentLine = line[:-1]
data = currentLine.split(" ")
# add item to the list
file_data.append(data)

# Decrease the first number in any line by one
for i in file_data:
if i[0].isdigit():
temp = float(i[0]) - 1
i[0] = str(int(temp))
# Write back to the file
f = open(item, 'w')
for i in file_data:
res = ""
for j in i:
res += j + " "
f.write(res)
f.write("n")
f.close()

这个程序读取一个文件并将任意一行的所有第一个数字减1。然后把它写回文件。

最新更新