Home > other >  Combine two wordlist in one file Python
Combine two wordlist in one file Python

Time:06-06

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!

CodePudding user response:

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.

CodePudding user response:

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

CodePudding user response:

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))

CodePudding user response:

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)

CodePudding user response:

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])

CodePudding user response:

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)

CodePudding user response:

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)

CodePudding user response:

You can use this one.

with open('wordlist1.txt')as f1,open('wordlist2.txt') as f2:
    l1 = f1.read().splitlines()
    l2 = f2.read().splitlines()

final = zip(l1,l2)

with open('wordlist3.txt', 'w') as out:
    for a,b in final:
        out.write(a '\n' b '\n')
  • Related