python两个文件合并|python3 如何中将两个文件按行合并

『壹』 python 合并多个 excel

思路利用python xlrd包读取excle文件,然后将文件内容存入一个列表中,再利用xlsxwriter将内容写入到一个新的excel文件中。完整代码 # -*- coding: utf-8 -*- #将多个Excel文件合并成一个import xlrdimport xlsxwriter #打开一个excel文件def open_xls(file): fh=xlrd.open_workbook(file) return fh #获取excel中所有的sheet表def getsheet(fh): return fh.sheets() #获取sheet表的行数def getnrows(fh,sheet): table=fh.sheets()[sheet] return table.nrows #读取文件内容并返回行内容def getFilect(file,shnum): fh=open_xls(file) table=fh.sheets()[shnum] num=table.nrows for row in range(num): rdata=table.row_values(row) datavalue.append(rdata) return datavalue #获取sheet表的个数def getshnum(fh): x=0 sh=getsheet(fh) for sheet in sh: x+=1 return x if __name__=='__main__': #定义要合并的excel文件列表 allxls=['F:/test/excel1.xlsx','F:/test/excel2.xlsx'] #存储所有读取的结果 datavalue=[] for fl in allxls: fh=open_xls(fl) x=getshnum(fh) for shnum in range(x): print("正在读取文件:"+str(fl)+"的第"+str(shnum)+"个sheet表的内容…") rvalue=getFilect(fl,shnum) #定义最终合并后生成的新文件 endfile='F:/test/excel3.xlsx' wb1=xlsxwriter.Workbook(endfile) #创建一个sheet工作对象 ws=wb1.add_worksheet() for a in range(len(rvalue)): for b in range(len(rvalue[a])): c=rvalue[a][b] ws.write(a,b,c) wb1.close() print("文件合并完成")

合并后

『贰』 python合并文件只读到一张表

具体如下:首先准备好要合并的文件,再新建一个文件,注意执行以下操作时要合并的文件要处于关闭状态。打开新建的文件,找到数据菜单栏中的获取和转换数据功能区,点击获取数据功能,在其下拉框中选择【自文件】-【从文件夹】。选择要合并的文件所在的文件夹路径。在弹出的窗口中选择右下角的【组合】-【合并和加载】。在弹出的合并文件窗口中点击确定,数据就被合并到新建的文件里面了。

『叁』 python 如何把多个文件内容合并到以一个文件

需要时使用pandas包

importpandasaspddf1=pd.read_csv('x1.txt',sep='',index_col=0)df1.columns=['f1']df2=pd.read_csv('x2.txt',sep='',index_col=0)df2.columns=['f2']rst=pd.concat([df1,df2],axis=1)rst.to_csv('rst.txt',sep='')

我假定你不同列的分隔符是制表符TAB,不是专的话可以修改属sep参数

『肆』 python 两个文件 A 和 B,各存放一行字母 要求把这两个文件中的信息合并,输出到新文件 C

with open('1.txt','r') as t: txt1=t.readline()with open('2.txt','r')as t: txt2=t.readline()txt3=txt1+txt2with open('3.txt','w')as obj: obj.write(txt3)三个文件分别是1.txt,2.txt,3.txt。第三个文件如果不存在,程序会自动创建一个

『伍』 python中将两个文件合并

你好:

其实这个问题不是很难啊:

请看代码:

txtpath1=r"a.txt"txtpath2=r"b.txt"txtpath3=r"c.txt"fpa=open(txtpath1)fpb=open(txtpath2)fpc=open(txtpath3,"w")arrB=[]forlinebinfpb.readlines():arrB.append(lineb)index=0forlineainfpa.readlines():index=index+1fpc.write(linea)foriinrange((index-1)*10,(index)*10):try:fpc.write(arrB[i])except:passprint"Done!"fpa.close()fpb.close()fpc.close()

『陆』 python 合并两个文件内容的问题

如果是大文件的话不适合将数据预先读入内存形成dict进行后期处理但有两个方法: 1. 用 key-value数据库如bsddb模拟内存中的dict2. 对两个文件先用系统工具软件进行排序(如linux下的sort),然后再同时打开两个文件在两个文件中顺序处理根据key值大小的不一移动不同的指针;然后进行合并逻辑。

『柒』 python3 如何中将两个文件按行合并

先将两个文件分别读取到两个列表中,再用循环输出到第3个文件。内

f_=open('e:/python27/a.txt','r')n=0list1=[]foriinf_.readlines():容n+=1s=i.strip()list1.append(s)f_.close()ff_=open('e:/python27/b.txt','r')m=0list2=[]foriinff_.readlines():m+=1s=i.strip()list2.append(s)ff_.close()fff_=open('e:/python27/c.txt','w')foriinrange(n):s=list1[i]+''+list2[i]fff_.write(s+'')print(s)fff_.close()

『捌』 python将两个文本合并起来

dd={}forlninopen('a.txt','r'):name,info=ln.strip().split('',1)dd[name]=infowithopen('c.txt','wt')ashandle:forlninopen('b.txt','r'):name=ln.stip()handle.write("%s%s"%name,dd.get(name,'0'))

『玖』 两个FASTA文件的合并 python

Python编程将两个文件合并,代码如下:

//例子:合并a.txt和版b.txt文件defreadf(filename):lines=file(filename).readlines()dic={}foriinlines:i_=i.split()dic[i_[0]]=int(i_[1])returndicdica=readf('a.txt')dicb=readf('b.txt')lines=[]foriindica:percent=str(float(dicb[i])*100/dica[i])+'%'s=''.join([i,str(dica[i]),str(dicb[i]),percent])s+=''lines.append(s)//合并成权c.txtwithopen('c.txt','w')asf:f.writelines(lines)f.close()

『拾』 用Python 将两个文件的内容合并成一个新的文件.

f1 = open("mit.txt", 'a+')f2 = open("unitcode.txt",'r')f3 = open("unitname.txt",'r')s2 = f2.read().replace('\n', '').split(',')s3 = f3.read().replace('\n', '').split(',')f1.write('Unit name\tUnit Codes\n')for i1, i2 in zip(s2, s3): f1.write("%s\t%s\n" % (i1.strip(), i2.strip()))f1.close()f2.close()f3.close()如果就是想让格式对的很齐的化,只要格式化输出就行了。用ljust的函数对齐就行了。


赞 (0)