简体   繁体   English

Function 使用一个文本中的一行和另一个文本中的一行来编写文本

[英]Function that writes a text using one line from one text and one line from another text

So, I have written a code that should write a text file using two other texts, which starts with a line of text from Text No1., then follows a line from Text No2., then follows a line of text from Text No1.因此,我编写了一个代码,该代码应该使用其他两个文本编写一个文本文件,该文本以 Text No1. 中的一行文本开头,然后是 Text No2. 中的一行,然后是 Text No1 中的一行文本。 etc. until at the end it should end with lines from the text file with the most lines, for example:等等,直到最后它应该以文本文件中行数最多的行结束,例如:

Text No1.文本 No1。 lines: a, b, c, d行:a、b、c、d

Text No2.文本 No2。 lines: e, f, g, h, i, j, k行:e、f、g、h、i、j、k

Text No3.文字 3。 lines (that should be written by program) a, e, b, f, c, h, d, i, j, k行(应由程序编写) a, e, b, f, c, h, d, i, j, k

But, at the moment, the code但是,目前,代码

def text1():
    f = open("textnr1.txt","r", encoding="UTF-8")
    for line in f:
        y = line.split(".")
        print(len(y))
        
    f.close()
    f = open("textnr2.txt","r", encoding="UTF-8")
    for line in f:
        k = line.split(".")
        print(len(k))
    f.close()
    
    gar=max(len(k),len(y))
    f = open("results.txt","a", encoding="UTF-8")
    for i in range(gar):
        if i<len(y):
            f.append(y[i])
        if i<len(k):
            f.append(k[i])
    f.close()

only works if those text files consist of only one line per text file.仅当这些文本文件每个文本文件仅包含一行时才有效。 What should I modify?我应该修改什么?

If both files are the same length you can simply use zip to get lines from both files at the same time and then simply write them both to the third file (also use context managers):如果两个文件的长度相同,您可以简单地使用zip同时从两个文件中获取行,然后将它们都写入第三个文件(也使用上下文管理器):

with open('file1.txt') as f1, open('file2.txt') as f2, \
     open('result.txt', 'w') as res:

    for l1, l2 in zip(f1, f2):
        res.write(f'{l1}\n{l2}')

You should fix some mistakes in your code:您应该修复代码中的一些错误:

  1. When you iterate through lines, you calculate len(y) , but len(y) is always 1 (of course if your line does not contain . ).当您遍历行时,您会计算len(y) ,但len(y)始终为 1(当然,如果您的行不包含. )。 I suppose you wanted to use len(y) as "count of lines in your file".我想您想使用len(y)作为“文件中的行数”。
  2. You should use write() method instead of append() .您应该使用write()方法而不是append()
def text1():
    f = open("files/textnr1.txt", "r", encoding="UTF-8")
    first_text_lines = f.readlines()
    f.close()

    f = open("files/textnr2.txt", "r", encoding="UTF-8")
    second_text_lines = f.readlines()
    f.close()

    gar = max(len(first_text_lines), len(second_text_lines))
    f = open("files/results.txt", "a", encoding="UTF-8")
    for i in range(gar):
        if i < len(first_text_lines):
            f.write(first_text_lines[i])
        if i < len(second_text_lines):
            f.write(second_text_lines[i])
    f.close()

You can merge the inputs in a list comprehension by leveraging itertools' chain and zip_longest:您可以通过利用 itertools 的链和 zip_longest 来合并列表推导中的输入:

text1 = "abcd"
text2 = "efghijk"

from itertools import chain,zip_longest

r = [ *filter(None,chain.from_iterable(zip_longest(text1,text2)))]

print(r)
['a', 'e', 'b', 'f', 'c', 'g', 'd', 'h', 'i', 'j', 'k']

text1 and text2 could be your k and y lists or any iterables (even more than 2 if needed) text1 和 text2 可以是您的 k 和 y 列表或任何可迭代对象(如果需要,甚至超过 2 个)

The above assumes that you have no empty lines in your input.以上假设您的输入中没有空行。 To handle empty lines, you'll need a different fillvalue for the shorter inputs:要处理空行,您需要为较短的输入使用不同的填充值:

noData = object()
r = [x for x in chain.from_iterable(zip_longest(text1,text2,fillvalue=noData)) 
             if x != noData] 

Currently you are overwriting your y and k variables for every line in file, so after the loop runs, they only have the last line.目前,您正在为文件中的每一行覆盖 y 和 k 变量,因此在循环运行后,它们只有最后一行。

if you need all the lines anyway, just do:如果您仍然需要所有线路,只需执行以下操作:

with open("textnr2.txt","r", encoding="UTF-8") as f:
    k=f.readlines()

this will take care of f.close() for you, and is the recommended way.这将为您处理 f.close() ,并且是推荐的方式。 k will be a list of all lines in file. k 将是文件中所有行的列表。

you say that you need lines, but in your code you are splitting by periods.你说你需要行,但在你的代码中你是按句点分割的。 I will assume that is because you wanted to test the function and your multiline reading didn't work.我假设这是因为您想测试 function 并且您的多行读取不起作用。

after you fix the file reading, the code for writing lines should probably work.修复文件读取后,编写行的代码应该可以工作。

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

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