-3

Im really new to programming and couldn´t find a satisfying answer so far. Im using python and I want to merge three textfiles receive all possible word combinations. I have 3 files:

First file:

line1
line2
line3

Second file(prefix):

pretext1
pretext2
pretext3

Third file(suffix):

suftext1
suftext2
suftext3

I already used .read() and have my variables containing the list for each textfile. Now I want to write a function to merge this 3 files to 1 and it should look like this:

outputfile:

pretext1 line1 suftext1 #this is ONE line(str)
pretext2 line1 suftext1
pretext3 line1 suftext1
pretext1 line1 suftext2
pretext1 line1 suftext3

and so on, you get the idea

I want all possible combinations in 1 textfile as output. I guess I have to use a loop within a loop?!

  • Maybe I'm missing something, but the method by which your strings should be merged seems non-obvious to me. – alexwlchan Jun 24 '15 at 11:59
  • Hi! I just want to create all possible combinations of those 3 word files. I tried to play around with itertools, but couldn´t get it to work. Basically I need to do the follwing: Pick 1st element/word of list1 and append 1st element/word of list 2 + append 1st element/word of list 3. Then move on with next element, etc... – Regis Cleanoris Jun 24 '15 at 12:25
  • `itertools.product` does exactly that. – Ian McLaird Jun 24 '15 at 15:14

2 Answers2

2

Here it is, if I got your question right. First you have to focus into the correct folder with the os package.

import os
os.chdir("The_path_of_the_folder_containing_the_files")

Then you open you three files, and put the words into lists:

file_1 = open("file_1.txt")
file_1 = file_1.read()
file_1 = file_1.split("\n")

file_2 = open("file_2.txt")
file_2 = file_2.read()
file_2 = file_2.split("\n")

file_3 = open("file_3.txt")
file_3 = file_3.read()
file_3 = file_3.split("\n")

You create the text you want in your output file with loops:

text_output = ""
for i in range(len(file_2)):
    for j in range(len(file_1)):
        for k in range(len(file_3)):
            text_output += file_2[i] + " " + file_1[j] + " " + file_3 [k] + "\n"

And you enter that text into your output file (if that file does not exist, it will be created).

file_output = open("file_output.txt","w")
file_output.write(text_output)
file_output.close()
ysearka
  • 3,805
  • 5
  • 20
  • 41
  • your iterations will outprint all possible combinations only for `file_1` lines – PYPL Jun 24 '15 at 13:23
  • I don't get your comment PYPL, i get in my output all 27 possible combinations of the 3 lines of each input file. Isn't that the aim? – ysearka Jun 24 '15 at 13:27
  • @Regis asked for all possible combinations for all files, thus it must be in total 27*3 outprinted combinations, but youre getting result only for 1 file – PYPL Jun 24 '15 at 13:28
  • Then my bad because I didn't get the goal of @Regis (I still don't see how you can have 27*3 combinations, for me it should be 3*3*3=27...) – ysearka Jun 24 '15 at 13:34
  • `resultLst.append(file_1[i] + " " + file_2[j] + " " + file_3 [k] + "\n")` `resultLst.append(file_1[i] + " " + file_3[j] + " " + file_2 [k] + "\n")` `resultLst.append(file_2[i] + " " + file_1[j] + " " + file_3 [k] + "\n")` `resultLst.append(file_2[i] + " " + file_3[j] + " " + file_1 [k] + "\n")` `resultLst.append(file_3[i] + " " + file_1[j] + " " + file_2 [k] + "\n")` `resultLst.append(file_3[i] + " " + file_2[j] + " " + file_1 [k] + "\n")` – PYPL Jun 24 '15 at 13:39
  • Ok I see now what you mean, but in his example he kept a special order in his output file. You see on every line the first word comes from file_2, the second from file_1, and the third from file_3 (so ligical btw). That's why I only put 27 combinations, because I think this order must be kept.@Regis could you specify if it does or not? Please. – ysearka Jun 24 '15 at 13:49
  • The logical order you spotted, isn´t really logical :P it was just an example. I could also have written it randomly like: pretext3+line2+suftext3. Just to make things clear: Assuming each file has 3 lines of text would lead to an output of 27 possible variations -> 3 * 3 *3 = 27 – Regis Cleanoris Jun 24 '15 at 14:12
  • It is WORKING :) The only thing I had to switch was file_1 with file_2 within the output code. Thank you guys so much, from here I can go ahead by myself – Regis Cleanoris Jun 24 '15 at 14:19
  • Well I just tried it with some text files, but I do get an Error: text_output += file_2[i] + " " + file_1[j] + " " + file_3 [k] + "\n" IndexError: list index out of range – Regis Cleanoris Jun 24 '15 at 14:28
  • Your three files do not have the same number of lines, I edited my text so that it works. Is that ok to you now? – ysearka Jun 24 '15 at 14:39
  • Yeah, its working now. I was wondering why you are using len(), because every text file in my finaly version could have x amount of lines. – Regis Cleanoris Jun 24 '15 at 14:48
  • with the .split(), I enter every line of your input files in the corresponding lists. And the len() in the loops make sure we take all the words in those lists. – ysearka Jun 24 '15 at 15:01
0

While the existing answer may be correct, I think this is a case where bringing in a library function is definitely the way to go.

import itertools

with open('lines.txt') as line_file, open('pretext.txt') as prefix_file, open('suftext.txt') as suffix_file:
    lines = [l.strip() for l in line_file.readlines()]
    prefixes = [p.strip() for p in prefix_file.readlines()]
    suffixes = [s.strip() for s in suffix_file.readlines()]

    combos = [('%s %s %s' % (x[1], x[0], x[2])) 
              for x in itertools.product(lines, prefixes, suffixes)]

    for c in combos:
        print c
Ian McLaird
  • 5,507
  • 2
  • 22
  • 31