python 文件合并和编号

时间:2024-01-13 15:27:08
 # -*- coding:utf-8 -*-
import os
import re
p1=r"([0-9][0-9][AB])\.\w{3}$"
pattern1=re.compile(p1)
def get_dir(zz):
listdir=[]
for filename in os.listdir('./%s'%zz):
listdir.append(filename)
# print(filename)
# print(type(filename))
# print(listdir)
return listdir def joint_b0(listdir,zz):
if not os.path.exists('./new/'):
os.makedirs('./new/')
fw=open('./new/%s.csv'%(zz),'a')
for i in listdir:
# print(i)
j=1000
matcher1=re.findall(pattern1,i)
fr=open('./%s/%s'%(zz,i))
for line in fr.readlines():
try:
# print(len(line))
if(len(line)==1):
continue
else:
fw.write(matcher1[0]+'_%s'%j+','+line.strip('\n')+'\n')
j=j+1
except:
pass
fr.close()
fw.close() def joint_b1(listdir,zz):#单独拼接
if not os.path.exists('./new/'):
os.makedirs('./new/')
fw=open('./new/%s.csv'%(zz),'a')
for i in listdir:
# print(i)
j=1
matcher1=re.findall(pattern1,i)
fr=open('./%s/%s'%(zz,i))
for line in fr.readlines():
fw.write(line) fr.close()
fw.close()
if __name__=="__main__": # dir_list=['b0'],'b3','b4','b2_idsd','b3_idcd' dir_list=['b4']#用于编号和拼接,会在new目录下生成编号号码的文件
for i in range(len(dir_list)):
listdir=get_dir(dir_list[i])
# print(listdir)
joint_b0(listdir,dir_list[i]) # dir_list=['stock']#用于拼接,将需要拼接的放到stock目录下,会在new目录下生成stock文件,然后根据需要修改名称
# for i in range(len(dir_list)):
# listdir=get_dir(dir_list[i])
# # print(listdir)
# joint_b1(listdir,dir_list[i])