我有2个文本文件,行数相同,但我的文件之间没有公共列。第一个文件有9列,第二个文件有8列。它们都有111557行。 2个文件中的行顺序是相同的,所以我只想简单地合并它们,这意味着我将有一个包含17列的文本文件。
这是第一个.txt文件的第一列(它们是标签限制的)
chr1 HAVANA gene 29554 31109 . + .
这是第二个.txt文件的第一行(它们是标签限制的)
ENSG00000243485.2 ENSG00000243485.2 lincRNA NOVEL MIR1302-11 lincRNA NOVEL MIR1302-11
答案 0 :(得分:3)
import csv
with open('path/to/file1') as infile1, open('path/to/file2') as infile2, open('path/to/output', 'w') as outfile:
writer = csv.writer(outfile, delimiter='\t')
for row1,row2 in zip(csv.reader(infile1, delimiter='\t'), csv.reader(infile2, delimiter='\t')):
writer.writerow(row1+row2)
答案 1 :(得分:2)
尝试这样:
with open('file1') as f1, open('file2') as f2, open('outfile','w') as out:
for x in f1:
y = next(f2)
out.write("{}\t{}".format(x.strip(),y))
如果您在上面收到错误,请尝试这样做:
f1 = open('file1')
f2 = open('file2')
out = open('outfile','w')
for x in f1:
y = next(f2)
out.write(x.strip() + "\t" + y)