Read random line from text file

Asked by Unuldinei

I have three text files:
male.txt - containing male names / names separator = new line
female.txt - containing female names / names separator = new line
results.txt - results file

I need:
loop:
male = "random line from male.txt"
female = "random line from female.txt"
write (str(male)+"-"+str(female)+"\n") to resuls.txt

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

import random

male = open("male.txt").readlines()
female = open("male.txt").readlines()
results = open("male.txt", "w")

howmany = len(male) * len(female)

for i in range(how many):
    print >> results, "%s - %s"%(random.choice(male).trim(), random.choice(male).trim())

Revision history for this message
Unuldinei (numaipotdeloc) said :
#2

Thanks RaiMan, that solved my question.