python实现文本文件合并
python合并文本文件示例代码。
python实现两个文本合并
employee文件中记录了工号和姓名
catemployee.txt:
100JasonSmith 200JohnDoe 300SanjayGupta 400AshokSharma
bonus文件中记录工号和工资
catbonus.txt:
100$5,000 200$500 300$3,000 400$1,250
要求把两个文件合并并输出如下,处理结果:
400ashoksharma$1,250 100jasonsmith$5,000 200johndoe$500 300sanjaygupta$3,000
这个应该是要求用shell来写的,但我的shell功底不怎么样,就用python来实现了
注意,按题目的意思,在输出文件中还需要按照姓名首字母来排序的
#!/usr/bin/envpython
#coding=utf-8
fp01=open("bonus.txt","r")
a=[]
forline01infp01:
a.append(line01)
fp02=open("employee.txt","r")
fc02=sorted(fp02,key=lambdax:x.split()[1])
forline02infc02:
i=0
whileline02.split()[0]!=a[i].split()[0]:
i+=1
print"%s%s%s%s"%(line02.split()[0],line02.split()[1],line02.split()[2],a[i].split()[1])
fp01.close()
fp02.close()
我们再来看一段同样功能的代码
#codinggbk
#
#author:GreatGhoul
#email:greatghoul@gmail.com
#blog:http://greatghoul.javaeye.com
importsys,os,msvcrt
defjoin(in_filenames,out_filename):
out_file=open(out_filename,'w+')
err_files=[]
forfileinin_filenames:
try:
in_file=open(file,'r')
out_file.write(in_file.read())
out_file.write('\n\n')
in_file.close()
exceptIOError:
print'errorjoining',file
err_files.append(file)
out_file.close()
print'joiningcompleted.%dfile(s)missed.'%len(err_files)
print'outputfile:',out_filename
iflen(err_files)>0:
print'missedfiles:'
print'--------------------------------'
forfileinerr_files:
printfile
print'--------------------------------'
if__name__=='__main__':
print'scanning...'
in_filenames=[]
file_count=0
forfileinos.listdir(sys.path[0]):
iffile.lower().endswith('[all].txt'):
os.remove(file)
eliffile.lower().endswith('.txt'):
in_filenames.append(file)
file_count=file_count+1
iflen(in_filenames)>0:
print'--------------------------------'
print'\n'.join(in_filenames)
print'--------------------------------'
print'%dpart(s)intotal.'%file_count
book_name=raw_input('enterthebookname:')
print'joining...'
join(in_filenames,book_name+'[ALL].TXT')
else:
print'nothingfound.'
msvcrt.getch()
最后我们再来看一个小编遇到的情况:
今天汇编的时候在阿甘的博客里面看到了一部小说《疯狂的程序员》,于是网上搜了下准备放到手机里闲时看看,无奈下载后发现是分章节的txt文本,一共有87个文件,考虑到阅读起来不是很方便,于是想找个现成的工具合并txt文本。
结果尝试了几个工具后觉得合并效果都不给力啊,于是打算自己动手。其实cmd的命令"type*.txt>>crazy-programmer.txt"还是很有效果的,然而合并后的txt文件却十分庞大,所以我还是自己写了一个脚本完成了合并。
说明:由于我下载的87个txt文件的字符编码格式都不统一,所以我用chardet模块判断字符编码类型后再用codecs模块的codecs.open功能解决了编码问题。如果直接用file的open打开txt文件的话,在UCS-2LittleEndian的编码情况下,file.read()遇到中文的冒号(即“:”)后会无法读取冒号以后的内容,所以需要用codecs.open(path,'r',encoding)来解决。
如果还有问题可以留言,代码如下:
#!coding:cp936
importcodecs,chardet
deffileopen(filename):
f=open(filename,'r')
s=f.read()
if(chardet.detect(s)['encoding']=='UTF-16LE'):
f.close()
f=codecs.open(filename,'r','utf-16-le')
data=f.read().encode('gb2312','ignore')
f.close()
elif(chardet.detect(s)['encoding']=='GB2312'):
data=s
f.close()
returndata
i=1
whilei<=87:
if(i<10):
filename='0'+str(i)+'.txt'
else:
filename=str(i)+'.txt'
text=fileopen(filename)
file('crazy-p.txt','a+').write(text)
i=i+1
其中,chardet模块需要下载安装,脚本还可以改进以适应更多种情况,我就懒了。