AI_python_yoooger/Ai_tottle/train/let_txt_to_true.py
2025-11-11 09:43:25 +08:00

86 lines
3.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
import stat
import math
def make_writable(file_path):
os.chmod(file_path, stat.S_IWRITE)
def process_files_in_folder(folder_path):
for root, _, files in os.walk(folder_path):
for file_name in files:
if file_name.endswith(".txt"):
file_path = os.path.join(root, file_name)
# 确保文件可写
make_writable(file_path)
# 读取文件内容并进行处理
with open(file_path, "r") as file:
lines = file.readlines()
processed_lines = []
for line in lines:
numbers = line.split()
processed_numbers = []
# 确保第一列为整数 0 或 1不处理为浮点数
if (
numbers[0] == "0"
or numbers[0] == "1"
or numbers[0] == "2"
or numbers[0] == "3"
or numbers[0] == "4"
or numbers[0] == "5"
or numbers[0] == "6"
or numbers[0] == "7"
or numbers[0] == "8"
or numbers[0] == "9"
or numbers[0] == "10"
or numbers[0] == "11"
or numbers[0] == "12"
or numbers[0] == "13"
or numbers[0] == "14"
or numbers[0] == "15"
or numbers[0] == "16"
or numbers[0] == "17"
or numbers[0] == "18"
):
processed_numbers.append(numbers[0])
else:
print(f"Unexpected value in first column: {numbers[0]}")
continue
# 处理后面的列,保留原始格式并确保负数变成正数,且删除 NaN 数据
skip_line = False # 用于标记是否跳过这一行
for number in numbers[1:]:
try:
number = float(number)
if math.isnan(number): # 检查是否为NaN
skip_line = True
print(
f"NaN detected in file: {file_path}, line: {line}"
)
break
if number < 0:
number = abs(number) # 将负数转换为正数
processed_numbers.append(str(number)) # 保留原始格式
except ValueError:
processed_numbers.append(number) # 非数字列保持原样
# 如果该行没有NaN数据则加入结果列表
if not skip_line:
processed_line = " ".join(processed_numbers)
processed_lines.append(processed_line)
# 将处理后的内容写回文件
with open(file_path, "w") as file:
file.write("\n".join(processed_lines))
print(f"Finished processing: {file_path}")
# 指定文件夹路径
folder_path = r"G:\dataset\PCS\before\labels"
#run the function
process_files_in_folder(folder_path)