私は3つの異なるテキストファイルと結合された新しいテキストファイルを作ろうとしています。コードについて詳しく説明します。Python)3つのテキストファイル(リスト)をソートして1つにまとめる
text a file = z
a
y
text b file = s
x
d
text c file = 1
3
2
ので、B、Cのテキストファイルがあり、そして私が作りたい:
text newfile: z s 1
y d 2
a x 3
あなたは上記を参照できるように、私はnewfileコマンドは「C」ファイルの順になりたいです。 以下は私が作ったものです。
def main():
a = open("text1","r")
b = open("text2","r")
c = open("text3","r")
text1list = []
text2list = []
text3list = []
for line1 in a:
line1 = line1.strip()
text1list.append(line1)
for line2 in b:
line2 = line2.strip()
text2list.append(line2)
for line3 in c:
line3 = line3.strip()
text3list.append(line3)
aa,bb,cc = zip(*sorted(zip(text3list, text1list, text2list)))
combine = list(zip(bb,cc,aa))
with open("finalfiles", 'w') as zzzz:
for item in combine:
zzzz.write("{}\n".format(item))
問題は今、私の出力は私のソートが機能している
('z','s','1')
('y','d','2')
('a','x','3')
ですが、それは私が期待するものとは異なります、です。 私はそれらと 'を望んでいません。それらがリストなので...だと思う? 私はこの時点で立ち往生しています。 さらに、私の並べ替えがうまくいくと教えてください!
書き込みの代わりにこの: '」」.join(項目)' –