ivy的依赖文件如果条目很多,不排序的话会很乱,例如:
(ori.txt中这里是空行)
排成这样就比较好看了:
实际上就是每行按字母顺序排序,手工排费时费力,下面的脚本就是实现这个功能的,将最上面的文本保存为e:/MyDoc/Project/IvyInstallScripts下的build_old_backup.txt(注意最后一行文字后面要有一个空行)中,运行此脚本,排好的文本保存在build_old_backup.txt中,未排序的原始文本保存在build_old_backup.bak中,如果文件夹下已经有build_old_backup.bak,则会首先删除此bak文件,再执行上面的操作:
import os
if name == 'main':
#name of the source file
srcFileName = 'e:/MyDoc/Project/IvyInstallScripts/build_old_backup'
haveXML = os.path.exists(srcFileName+'.txt')
haveBAK = os.path.exists(srcFileName+'.bak')
if haveXML and haveBAK:
os.remove(srcFileName+'.bak')
if haveXML:
os.rename(srcFileName+'.txt', srcFileName+'.bak')
haveBAK = True
if not haveBAK:
print("no source file, quit now!")
exit()
# open the src file
oriScript = open(srcFileName+'.bak','rt')
#the destination file
destScript = open(srcFileName+'.txt','wt')
try:
wholeTxt = []
for curLine in oriScript:
#print(len(curLine))
wholeTxt.append(curLine)
wholeTxt.sort()
for curLine in wholeTxt:
destScript.write(curLine)
finally:
oriScript.close()
destScript.close()
下面是简单版:更为易用,保存为一个.py文件即可。
"""
将文本中的各行按字母顺序排序。使用方法:
将待排序的文本保存在脚本所在目录下,运行脚本,结果保存在dst.txt文件中。
"""
import os
oriScript = open('./ori.txt','rt')
dstScript = open('./dst.txt','wt')
try:
wholeTxt = [] for curLine in oriScript: wholeTxt.append(curLine) noDupList = list(set(wholeTxt)) # delete potential duplicated lines in wholeTxt noDupList.sort() for curLine in noDupList: dstScript.write(curLine)
finally:
oriScript.close()
dstScript.close()