在Python中逐行组合多个文本文件
假设我有文本文件a、b、c 文件a在Python中逐行组合多个文本文件,python,linux,file-io,Python,Linux,File Io,假设我有文本文件a、b、c 文件a 1 2 3 文件b a b c 文件c d e f 我想写一个输出文件,如: 1 a d 2 b e 3 c f 是否要用python或Linux中的任何命令编写它?您可以使用zip实现此目的: with open("filea") as f1, open("fileb") as f2, open("filec") as f3: for a, b, c in zip(f1, f2, f3): print " ".
1
2
3
文件b
a
b
c
文件c
d
e
f
我想写一个输出文件,如:
1 a d
2 b e
3 c f
是否要用python或Linux中的任何命令编写它?您可以使用
zip
实现此目的:
with open("filea") as f1, open("fileb") as f2, open("filec") as f3:
for a, b, c in zip(f1, f2, f3):
print " ".join(map(str.rstrip, (a, b, c)))
您可以使用
zip
进行以下操作:
with open("filea") as f1, open("fileb") as f2, open("filec") as f3:
for a, b, c in zip(f1, f2, f3):
print " ".join(map(str.rstrip, (a, b, c)))
在命令行中:
$ paste a b c
1 a x
2 b y
3 c z
在命令行中:
$ paste a b c
1 a x
2 b y
3 c z
事实上有一千条线,不仅仅是a,b,c。事实上有一千条线,不仅仅是a,b,c。