如何基于Python按行合并两个Txt

环境python3.6

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

E35A323F-2B54-62D3-31E6-9561E9A96CC9.png36FF5F92-D206-A893-9161-FE653BAD4C6B.png

代码

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)

合并后的文件

9122174F-53B5-098F-9985-8E5BC97F23AE.png

收藏 (0)
评论列表
正在载入评论列表...
我是有底线的