Python]将两个文本文件合并为一个(逐行)
我是python新手。我要做的是将文件“a”和文件“b”逐行组合成一个文件 比如说,Python]将两个文本文件合并为一个(逐行),python,Python,我是python新手。我要做的是将文件“a”和文件“b”逐行组合成一个文件 比如说, 文本文件a=a(“\n”)b(“\n”)c text file b = 1("\n")2("\n") 3 新文本文件将包含a1(“\n”)b2(\n”)c3 def main(): f1 = open("aaa.txt") f2 = f1.readlines() g1 = open("bbb.txt") g2 = g1.readlines() h1 = f2+g2 print(h1)
文本文件a=a(“\n”)b(“\n”)c
text file b = 1("\n")2("\n") 3
新文本文件将包含a1(“\n”)b2(\n”)c3
def main():
f1 = open("aaa.txt")
f2 = f1.readlines()
g1 = open("bbb.txt")
g2 = g1.readlines()
h1 = f2+g2
print(h1)
很抱歉,这是我第一次使用stackoverflow..阅读有关文件处理和高效使用内置函数的更多信息。 对于您的查询,仅使用
h1=f2+g2
是不合适的
a=open('a.txt','r').readlines()
b=open('b.txt','r').readlines()
with open('z.txt','w') as out:
for i in range(0,10): #this is for just denoting the lines to join.
print>>out,a[i].rstrip(),b[i]
要点:
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
- 使用
打开文件。不需要关闭文件和
- 使用
函数组合两个列表李>zip
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
x.txt的内容:
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
y.txt的内容:
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
z.txt的内容:
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
带有zip函数的代码:
combine =[]
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists
#combine = list(zip(ylines,xlines))
#Write to third file
for i in range(len(xlines)):
line = ylines[i].strip() + ' ' + xlines[i]
zh.write(line)
1
2
3
a
b
c
a 1
b 2
c 3
with open("x.txt") as xh:
with open('y.txt') as yh:
with open("z.txt","w") as zh:
#Read first file
xlines = xh.readlines()
#Read second file
ylines = yh.readlines()
#Combine content of both lists and Write to third file
for line1, line2 in zip(ylines, xlines):
zh.write("{} {}\n".format(line1.rstrip(), line2.rstrip()))
您尝试了什么?谢谢您的回复。我尝试了很多不同的东西,也许你会嘲笑我:(我想出了如何制作一个(“\n”)b(“\n”)c(“\n”)1(“\n”)2(“\n”)3),但我在谷歌上找不到任何地方(一行一行):(欢迎使用StackOverFlow user6886108!!!请查看此链接-至少向我们展示打开两个文件并打印的代码Linessory伙计们这是我第一次问有关Stackflow的问题,感谢你提供的链接Dinesh。哇..你让我开心了一天。我从昨天开始就一直在处理此问题。要根据你所做的内容编写新文件,请告诉我们我知道我应该从newfile=open(“new”,“w”)开始,如果行数超过10行怎么办?@user6886108@Dinesh而不是10行,你可以使用行数最少的文件a或文件b的len。当你使用的行数比另一行多的文件时。这行(
print a[I].rstrip(),b[I]
)将抛出错误。@user6886108修改了代码,将其作为z.txt
存储在单独的文件中。谢谢。如果我想使z.txt成为1(“\n”)b2(“\n”)c3(“\n”)?而不是(“\n”)1(“\n”)b(“\n”)2(“\n”)c(“\n”)3检查更新后的代码永远不知道“使用打开文件。无需关闭文件”谢谢!这对我很有帮助much@DineshPundkar很高兴看到您编辑的代码,范围而不是组合。捕捉得很好。