I would like to take a CSV file with each column being a category of words and generate all possible combinations among them. Here is the simplified CSV file I am using to test the script (but the CSV that will be used will be larger, with a dozen or more columns):
$ cat file.csv
Color,Pet,Action
black,dog,barks
brown,cat,runs
white,bird,flies
,hamster,
red,,swims
As you can see, some columns have more words (i.e. there could be more "colors" than "pets", or more "pets" than "actions" for example).
Here's what I have so far:
import csv
import itertools
with open('file.csv', newline='') as csvfile:
next(csvfile, None) #skip header row
data = list(csv.reader(csvfile))
for combination in itertools.product(*data):
print(combination)
And here's an excerpt of the output I am getting:
$ python3 combiner.py
('black', 'brown', 'white', '', 'red')
('black', 'brown', 'white', '', '')
('black', 'brown', 'white', '', 'swims')
('black', 'brown', 'white', 'hamster', 'red')
('black', 'brown', 'white', 'hamster', '')
('black', 'brown', 'white', 'hamster', 'swims')
('black', 'brown', 'white', '', 'red')
('black', 'brown', 'white', '', '')
('black', 'brown', 'white', '', 'swims')
('black', 'brown', 'bird', '', 'red')
('black', 'brown', 'bird', '', '')
[...]
What I would like to accomplish:
- not have multiple items from the same category (column) in the same output line
- removing parentheses, quotes and commas (I believe I can accomplish that by converting the array to a string before printing)
So, to give an example of the output I am trying to get:
black
black dog
black dog barks
black dog runs
black dog flies
black dog swims
black cat
black cat barks
black cat runs
black cat flies
black cat swims
brown
brown dog
brown dog barks
[...]
black hamster
black hamster flies
[...]
red fish runs
[...]
If anyone has a suggestion on the most efficient way to accomplish this (or a specific library or approach to take), I would appreciate it greatly.