将两个单词列表组合在一个文件python中
根据以下示例,我有两个单词列表:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试始终尝试包括您尝试过的内容。
但是,这是一个很好的起点。
检查以下问题以获取更多想法和理解:如何将逐线读取到列表中?
欢呼声
Try to always try to include what you have tried.
However, this is a great place to start.
Check the following question for more ideas and understanding: How to read a file line-by-line into a list?
Cheers
打开WordList1.txt和WordList2.txt用于读取和WordList3.txt编写。然后,它很简单:
Open wordlist1.txt and wordlist2.txt for reading and wordlist3.txt for writing. Then it's as simple as:
您还可以直接在文件上迭代,而不是使用
.splitlines()
。这是代码:在第一个块中,我们正在打开文件。对于前两个,我们使用
“ r”
,代表读取,因为我们不想将任何内容更改为文件。我们可以忽略这一点,因为“ r”是打开函数的默认参数。对于第二个,我们使用“ W”
,代表写。如果该文件还不存在,它将创建一个新文件。接下来,我们在for循环中使用
zip
函数。它创建了一个迭代器,该迭代器包含来自所有作为参数的迭代的元素。在此循环中,它将包含包含WordList1.txt
和WordList2.txt
的每行的元组。这些元组直接将变量解开txt1
和txt2
。接下来,我们使用if语句检查
WordList1.txt
的行是否以newline结束。最后一行可能并非如此,因此需要检查这一点。我们不使用第二行检查它,因为最后一行没有新线,因为它也将在结果文件的末尾。接下来,我们将文本写入
wordlist3.txt
。这意味着将文本附加到文件末尾。但是,丢失了在开口之前文件中已经存在的文本。最后,我们关闭文件。这是非常重要的,因为否则某些进度可能无法保存,并且没有其他应用程序可以使用该文件。
Instead of using
.splitlines()
, you can also iterate over the files directly. Here's the code: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 ofwordlist1.txt
and one ofwordlist2.txt
. These tuples are directly unpacked into the variablestxt1
andtxt2
.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.
尝试以下操作:
Try this:
干得好 :)
Here you go :)
打开WordList 1和2并制作线条,用Newline字符将每个对分开,然后将所有对结合在一起,然后通过Newline再次分离。
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.