将两个单词列表组合在一个文件python中

发布于 2025-02-04 10:35:27 字数 316 浏览 2 评论 0原文

根据以下示例,我有两个单词列表:

WordList1.txt

aa
bb
cc

wordlist2.txt

11
22
33

我想从WordList2.txt中获取所有行,并在WordList1中的每一行之后。 TXT并将它们混合在WordList3.txt中:

aa
11
bb
22
cc
33
.
.

您能帮我如何做吗?谢谢!

I have two wordlists, as per the examples below:

wordlist1.txt

aa
bb
cc

wordlist2.txt

11
22
33

I want to take every line from wordlist2.txt and put it after each line in wordlist1.txt and combine them in wordlist3.txt like this:

aa
11
bb
22
cc
33
.
.

Can you please help me with how to do it? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冰雪梦之恋 2025-02-11 10:35:29

尝试始终尝试包括您尝试过的内容。

但是,这是一个很好的起点。

def read_file_to_list(filename):
  with open(filename) as file:
      lines = file.readlines()
      lines = [line.rstrip() for line in lines]
  return lines

wordlist1= read_file_to_list("wordlist1.txt")
wordlist2= read_file_to_list("wordlist2.txt")

with open("wordlist3.txt",'w',encoding = 'utf-8') as f:
  for x,y in zip(wordlist1,wordlist2):
    f.write(x+"\n")
    f.write(y+"\n")

检查以下问题以获取更多想法和理解:如何将逐线读取到列表中?

欢呼声

Try to always try to include what you have tried.

However, this is a great place to start.

def read_file_to_list(filename):
  with open(filename) as file:
      lines = file.readlines()
      lines = [line.rstrip() for line in lines]
  return lines

wordlist1= read_file_to_list("wordlist1.txt")
wordlist2= read_file_to_list("wordlist2.txt")

with open("wordlist3.txt",'w',encoding = 'utf-8') as f:
  for x,y in zip(wordlist1,wordlist2):
    f.write(x+"\n")
    f.write(y+"\n")

Check the following question for more ideas and understanding: How to read a file line-by-line into a list?

Cheers

娇纵 2025-02-11 10:35:29

打开WordList1.txt和WordList2.txt用于读取和WordList3.txt编写。然后,它很简单:

with open('wordlist3.txt', 'w') as w3, open('wordlist1.txt') as w1, open('wordlist2.txt') as w2:
    for l1, l2 in zip(map(str.rstrip, w1), map(str.rstrip, w2)):
        print(f'{l1}\n{l2}', file=w3)

Open wordlist1.txt and wordlist2.txt for reading and wordlist3.txt for writing. Then it's as simple as:

with open('wordlist3.txt', 'w') as w3, open('wordlist1.txt') as w1, open('wordlist2.txt') as w2:
    for l1, l2 in zip(map(str.rstrip, w1), map(str.rstrip, w2)):
        print(f'{l1}\n{l2}', file=w3)
一个人的夜不怕黑 2025-02-11 10:35:29

您还可以直接在文件上迭代,而不是使用.splitlines()。这是代码:

wordlist1 = open("wordlist1.txt", "r")
wordlist2 = open("wordlist2.txt", "r")
wordlist3 = open("wordlist3.txt", "w")

for txt1,txt2 in zip(wordlist1, wordlist2):
    if not txt1.endswith("\n"):
        txt1+="\n"
    wordlist3.write(txt1)
    wordlist3.write(txt2)

wordlist1.close()
wordlist2.close()
wordlist3.close()

在第一个块中,我们正在打开文件。对于前两个,我们使用“ r”,代表读取,因为我们不想将任何内容更改为文件。我们可以忽略这一点,因为“ r”是打开函数的默认参数。对于第二个,我们使用“ W”,代表写。如果该文件还不存在,它将创建一个新文件。

接下来,我们在for循环中使用zip函数。它创建了一个迭代器,该迭代器包含来自所有作为参数的迭代的元素。在此循环中,它将包含包含WordList1.txtWordList2.txt的每行的元组。这些元组直接将变量解开txt1txt2

接下来,我们使用if语句检查WordList1.txt的行是否以newline结束。最后一行可能并非如此,因此需要检查这一点。我们不使用第二行检查它,因为最后一行没有新线,因为它也将在结果文件的末尾。

接下来,我们将文本写入wordlist3.txt。这意味着将文本附加到文件末尾。但是,丢失了在开口之前文件中已经存在的文本。

最后,我们关闭文件。这是非常重要的,因为否则某些进度可能无法保存,并且没有其他应用程序可以使用该文件。

Instead of using .splitlines(), you can also iterate over the files directly. Here's the code:

wordlist1 = open("wordlist1.txt", "r")
wordlist2 = open("wordlist2.txt", "r")
wordlist3 = open("wordlist3.txt", "w")

for txt1,txt2 in zip(wordlist1, wordlist2):
    if not txt1.endswith("\n"):
        txt1+="\n"
    wordlist3.write(txt1)
    wordlist3.write(txt2)

wordlist1.close()
wordlist2.close()
wordlist3.close()

In the first block, we are opening the files. For the first two, we use "r", which stands for read, as we don't want to change anything to the files. We can omit this, as "r" is the default argument of the open function. For the second one, we use "w", which stands for write. If the file didn't exist yet, it will create a new file.

Next, we use the zip function in the for loop. It creates an iterator containing tuples from all iterables provided as arguments. In this loop, it will contain tuples containing each one line of wordlist1.txt and one of wordlist2.txt. These tuples are directly unpacked into the variables txt1 and txt2.

Next we use an if statement to check whether the line of wordlist1.txt ends with a newline. This might not be the case with the last line, so this needs to be checked. We don't check it with the second line, as it is no problem that the last line has no newline because it will also be at the end of the resulting file.

Next, we are writing the text to wordlist3.txt. This means that the text is appended to the end of the file. However, the text that was already in the file before the opening, is lost.

Finally, we close the files. This is very important to do, as otherwise some progress might not be saved and no other applications can use the file meanwhile.

不打扰别人 2025-02-11 10:35:29

尝试以下操作:

with open('wordlist1.txt', 'r') as f1:
    f1_list = f1.read().splitlines()

with open('wordlist2.txt', 'r') as f2:
    f2_list = f2.read().splitlines()

f3_list = [x for t in list(zip(f1, f2)) for x in t]

with open('wordlist3.txt', 'w') as f3:
    f3.write("\n".join(f3_list))

Try this:

with open('wordlist1.txt', 'r') as f1:
    f1_list = f1.read().splitlines()

with open('wordlist2.txt', 'r') as f2:
    f2_list = f2.read().splitlines()

f3_list = [x for t in list(zip(f1, f2)) for x in t]

with open('wordlist3.txt', 'w') as f3:
    f3.write("\n".join(f3_list))
长安忆 2025-02-11 10:35:29
with open('wordlist1.txt') as w1,\
        open('wordlist2.txt') as w2,\
        open('wordlist3.txt', 'w') as w3:

    for wordlist1, wordlist2 in zip(w1.readlines(), w2.readlines()):
        if wordlist1[-1] != '\n':
            wordlist1 += '\n'
        if wordlist2[-1] != '\n':
            wordlist2 += '\n'

        w3.write(wordlist1)
        w3.write(wordlist2)
with open('wordlist1.txt') as w1,\
        open('wordlist2.txt') as w2,\
        open('wordlist3.txt', 'w') as w3:

    for wordlist1, wordlist2 in zip(w1.readlines(), w2.readlines()):
        if wordlist1[-1] != '\n':
            wordlist1 += '\n'
        if wordlist2[-1] != '\n':
            wordlist2 += '\n'

        w3.write(wordlist1)
        w3.write(wordlist2)
世界和平 2025-02-11 10:35:29

干得好 :)

 with open('wordlist1.txt', 'r') as f:
    file1 = f.readlines()

with open('wordlist2.txt', 'r') as f:
    file2 = f.readlines()

with open('wordlist3.txt', 'w') as f:
    for x in range(len(file1)):
        if not file1[x].endswith('\n'):
            file1[x] += '\n'
        f.write(file1[x])
        if not file2[x].endswith('\n'):
            file2[x] += '\n'
        f.write(file2[x])

Here you go :)

 with open('wordlist1.txt', 'r') as f:
    file1 = f.readlines()

with open('wordlist2.txt', 'r') as f:
    file2 = f.readlines()

with open('wordlist3.txt', 'w') as f:
    for x in range(len(file1)):
        if not file1[x].endswith('\n'):
            file1[x] += '\n'
        f.write(file1[x])
        if not file2[x].endswith('\n'):
            file2[x] += '\n'
        f.write(file2[x])
杀手六號 2025-02-11 10:35:29

打开WordList 1和2并制作线条,用Newline字符将每个对分开,然后将所有对结合在一起,然后通过Newline再次分离。

# paths
wordlist1 = #
wordlist2 = #
wordlist3 = #

with open(wordlist1, 'r') as fd1, open(wordlist2, 'r') as fd2:
    out = '\n'.join(f'{l1}\n{l2}' for l1, l2 in zip(fd1.read().split(), fd2.read().split()))

with open(wordlist3, 'w') as fd:
    fd.write(out)

Open wordlist 1 and 2 and make a line paring, separate each pair by a newline character then join all the pairs together and separated again by a newline.

# paths
wordlist1 = #
wordlist2 = #
wordlist3 = #

with open(wordlist1, 'r') as fd1, open(wordlist2, 'r') as fd2:
    out = '\n'.join(f'{l1}\n{l2}' for l1, l2 in zip(fd1.read().split(), fd2.read().split()))

with open(wordlist3, 'w') as fd:
    fd.write(out)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文