Python合并两个txt按行合并

环境python3.6

合并前第一个文件和第二个文件

 

代码

file1 = open("my.txt","r")
file2 = open("ids.txt","r")

file1_lists =file1.readlines()
file2_lists =file2.readlines()

file3_list = []
file4_list = []
for i in file1_lists:
    temp_list = i.split()
    file3_list.append(str(temp_list[0]))
# print(len(file3_list))

for i in file2_lists:
    temp_list = i.split()
    file4_list.append(str(temp_list[0]))
# print(len(file4_list))
main_file = []

for i in range(len(file3_list)):
    s=""
    s = "".join([file3_list[i],file4_list[i]])
    s+="\n"
    main_file.append(s)
file3 = open("three.txt","w")
file3.writelines(main_file)

 

合并后的文件

 

posted @ 2020-10-27 08:32  15号计划  阅读(907)  评论(0编辑  收藏  举报