0

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!

HOH_HOH
  • 105
  • 1
  • 2
  • 12
  • 2
    What do you want to do if the files are of different sizes (number of lines)? What have you tried so far? Do you know how to open files for reading and writing? – DarkKnight Jun 05 '22 at 18:53
  • @AlbertWinestein I know how this works but not working, Also it dosen't matter if number of lines are diffrent. this is for equal wordlist... – HOH_HOH Jun 05 '22 at 18:58

7 Answers7

1

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)
DarkKnight
  • 19,739
  • 3
  • 6
  • 22
1

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

0

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])
  • 1
    If you use [`with`](https://www.bing.com/ck/a?!&&p=9d5843e08417541586ecffc62a1ec026ba0c51c9ae3808331ec1c0270d9ea052JmltdHM9MTY1NDQ1NTkzNyZpZ3VpZD1jNTU1NzJkZC05MzM0LTRmZTktYmRiNC1kZGQzOGUwMTAyOGImaW5zaWQ9NTQ0OA&ptn=3&fclid=7bdca25b-e502-11ec-956f-1c94b3ec86bf&u=a1aHR0cHM6Ly9pcS5vcGVuZ2VudXMub3JnL3dpdGgtaW4tcHl0aG9uLyM6fjp0ZXh0PVdoYXQlMjBpcyUyMHRoZSUyMFdpdGglMjBzdGF0ZW1lbnQlM0YlMjBUaGUlMjBXaXRoJTIwc3RhdGVtZW50LHJlbGVhc2UlMjBvZiUyMHJlc291cmNlcyUyMHRvJTIwYXZvaWQlMjBibG9ja2luZyUyMG90aGVyJTIwcHJvY2Vzc2VzLg&ntb=1) statement then you don't need to close the file it is automatically close by python. – codester_09 Jun 05 '22 at 19:06
  • 1
    I learn every day, will make a mental note of this :D –  Jun 05 '22 at 19:07
  • No you are good – HOH_HOH Jun 05 '22 at 19:09
0

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))
JMA
  • 803
  • 4
  • 9
0
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)
BhusalC_Bipin
  • 801
  • 3
  • 12
0

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.

The_spider
  • 1,202
  • 1
  • 8
  • 18
  • "no problem that the last line has no newline" That, of course, is exactly the kind of thinking that creates the problem of having to handle newline-less last lines in the first place... – Kelly Bundy Jun 05 '22 at 19:16
  • Any program should be able to handle them, as files might also be typed by humans. There is thus no good reason for adding abundant code. Files having an extra newline might also look a bit weird to inexperienced users. – The_spider Jun 05 '22 at 19:20
0

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)
cards
  • 3,936
  • 1
  • 7
  • 25