因为windows和linux换行符的问题,把数据拿给windows机器进行处理要进行换行符的切换。
windows的换行符是’\r\n’,linux的换行符是’\n’
sed 命令
首先想到的是使用sed命令,但是发现mac上的sed只能置换每一行空格前的第一个字符。于是使用sed命令没有换行成功。
find命令用来解决如上图所示的参数list超过上限的问题
find ./3-label -name "*.txt" -exec sed -i '' 's/\n/\r\n/' {} \;-print
还要注意mac上的sed命令加了-i之后后面要加’’,如果有备份文件引号里就填备份文件的内容,如果没有就为空。才能在mac上正常运行。
换行脚本
于是无奈只好自己编写一个脚本来处理了。
#-*-encoding:utf-8 -*-
import os
import glob
import re
import argparse
import time
from collections import namedtuple
import sys
import shutil
import random
def read_files(root_label_path):
#print("reading label files....")
files = []
for line in glob.iglob(os.path.join(root_label_path, "**", "*.txt"), recursive=True):
#for line in glob.iglob(os.path.join(root_label_path,"**"), recursive=True):
if re.search(r'classes.txt', line):
continue
files.append(line)
return list(files)
def convert_linebreak_to_windows(labels):
for label_file in labels:
if re.search(r"classes.txt", label_file):
continue
boxes = []
with open(label_file) as f:
for line in f.readlines():
line = line.replace('\n','')
#line = line + '\r\n'
boxes.append(line)
#label_filename = label_file.replace('3/', '')
#labelpath = "3-labels/" + label_filename
with open(label_file, 'w') as file:
for l in boxes:
#print("writing line {}".format(l))
file.write(l + '\r\n')
def convert_linebreak_to_linux(labels):
for label_file in labels:
if re.search(r"classes.txt", label_file):
continue
boxes = []
with open(label_file) as f:
for line in f.readlines():
line = line.replace('\r\n','')
#line = line + '\r\n'
boxes.append(line)
#label_filename = label_file.replace('3/', '')
#labelpath = "3-labels/" + label_filename
with open(label_file, 'w') as file:
for l in boxes:
#print("writing line {}".format(l))
file.write(l + '\n')
if __name__ == '__main__':
labels = read_files(sys.argv[1])
### linux
#convert_linebreak_to_linux(labels)
### windows
convert_linebreak_to_windows(labels)