简体   繁体   English

python将两个文本文件合并到一个文件中

[英]python combine two text files to one file

i want to create a simple code to combine two text files , example file1.txt contain: 我想创建一个简单的代码来组合两个文本文件,例如file1.txt包含:

car
house

and file2.txt contain : 和file2.txt包含:

voiture
maison

i want to combine the lines of the two files and separte them by ':' to look like that : 我想组合两个文件的行,并用':'将它们分开,看起来像这样:

car:voiture 
house:maison 

i try to do it and i'm sure that i'm wrong anyway i will post my code :) : 我试着这样做,我确定我错了,无论如何我会发布我的代码:):

with open("user.txt") as u: 
 with open("site.txt") as s:
     for line in s.read().split('\n'):
      s1=line
      for line in u.read().split('\n'):
       s2=line
       with open('result.txt', 'a') as file:
        file.write(s1+':'+s2)

and thanks a lot for any help guys :) 非常感谢任何帮助人:)

This is a use case for itertools.izip : 这是itertools.izip一个用例:

from itertools import izip

with open('file1.txt') as f1, open('file2.txt') as f2, open('new.txt', 'w') as fout:
    for fst, snd in izip(f1, f2):
        fout.write('{0}:{1}\n'.format(fst.rstrip(), snd.rstrip()))

This combines the first line from the first file with the first line from the second file (then the second line from the first file with the second line from the second file etc...), removes newlines from the lines, adds a : in the middle and adds a \\n so it's actually a line. 这将第一个文件中的第一行与第二个文件中的第一行(然后是第一个文件中的第二行和第二个文件中的第二行等)组合在一起,从行中删除换行符,添加:中间并添加\\n所以它实际上是一条线。 This saves loading both files fully into memory and does it iteratively over each. 这样可以节省将两个文件完全加载到内存中,并在每个文件上迭代执行。 Note though, that if the files are not of equal length, the result will stop at the number of lines in the shortest file. 但请注意,如果文件的长度不相等,则结果将停在最短文件中的行数。

Your code won't work because you try to read the whole second file for every line in the first one, also, there are a few other bugs (like not writing newlines, etc.). 您的代码将无法正常工作,因为您尝试读取第一个文件中的每一行的第二个文件,还有一些其他错误(例如不写新行等)。 Try instead 试试吧

with open("user.txt") as u, \
     open("site.txt") as s, \
     open("result.txt", "a") as file: # Only open every file once for all output 
     for s1 in u: # You don't nead to use .read().split('\n')
         s2 = s.readline() # Read ONE line INCLUDING the newline char
         file.write(s1 + ":" + s2) # Write output with the newline from `s2`

Or, you could just read all lines and use zip : 或者,您可以只阅读所有行并使用zip

with open("user.txt") as u, \
     open("site.txt") as s, \
     open("result.txt", "a") as file:
     user_lines = u.readlines()
     size_lines = s.readlines()
     for s1, s2 in zip(user_lines, size_lines):
         file.write(s1 + ":" + s2 + "\n") # Write output with newline char

You're close, but there's a problem. 你很亲密,但有一个问题。 Rather than iterating one after the other or one inside another, you need to iterate them both together. 您需要将它们一起迭代,而不是一个接一个地迭代或一个接一个地迭代。 Fortunately, that's easy with zip : 幸运的是,使用zip很容易:

with open('user.txt', 'r') as user_file, \
     open('site.txt', 'r') as site_file, \
     open('result.txt', 'a') as result_file:
    user_lines = user_file.read().split('\n')
    site_lines = site_file.read().split('\n')
    for user_line, site_line in zip(user_lines, site_lines):
        result_file.write(user_line + ':' + site_line + '\n')

If you do that you'll read all of file u before file s get's to the second line. 如果你这样做,你将在文件到达第二行之前读取所有文件。

You want to use file.readline() to iterate each file by one line at a time until you finish one or both files (and probably have something to deal with that last uneven line if the files aren't equal size.) 您希望使用file.readline()一次一行地迭代每个文件,直到您完成一个或两个文件(如果文件大小不同,可能需要处理最后一行不正确的行。)

My solution: 我的解决方案

#!/usr/bin/env python
# -*- coding: utf-8 -*-

with open('result.txt', 'a') as r:
    with open("user.txt") as u: 
        with open("site.txt") as s:
            for line in u:
                r.write(
                    "{0}: {1}".format(
                        line[:-1] if line.endswith('\n') else line,
                        s.readline()
                    )
                )

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM