import os
for root, dirs, files in os.walk(r""): # 文件夹路径
for file in files:
# 获取文件name
print(root)
print(file)
root1 = str(root+'/'+file) # 需要转换文件的路径
f = open(root1) # 打开txt文件
line = f.readlines() # 以行的形式进行读取文件
list1 = []
for i,line in enumerate(line):
a = line.split()
b = a[0:5] # 这是选取需要读取/修改的列;txt文件内一共五列
list1.append(b) # 将其添加在列表之中
f.close()
root2 = str(r''+'/'+file) # 转换后存放文件夹路径+转换后新的txt文件名称
path_out = root2
with open(path_out, 'w+') as f_out:
for i in list1:
fir = '' # 第一列变成所需要的类别
sec = i[1] # 第二、三、四、五列内容保持不变
third = i[2]
fourth = i[3]
fifth = i[4]
f_out.write(fir + ' ' + str(sec) + ' ' + str(third) + ' ' + str(fourth) + ' ' + str(fifth) + '\n') # 把前两列写入新的txt文件
Changing the data category of the first column can reduce the workload