-6

i have this input text file bio.txt

Enter for a chance to {win|earn|gain|obtain|succeed|acquire|get} 
1⃣Click {Link|Url|Link up|Site|Web link} Below️
2⃣Enter Name
3⃣Do the submit(inside optin {put|have|positioned|set|placed|apply|insert|locate|situate|put|save|stick|know|keep} {shipping|delivery|shipment} adress)

need locate syntax like this {win|earn|gain|obtain|succeed|acquire|get} and return random word, example : win

how i can locate this in python started from my code :

input = open('bio.txt', 'r').read()
SEOExpert
  • 3
  • 1

2 Answers2

0

You can search for your pattern ("\{.*\}" according to your example) with regex on each line. Then once you found it, simply split the match by a separator ("|" according to your example). finally return randomly an element of the list.

Regular expression doc : https://docs.python.org/2/library/re.html

Python's string common operation doc (including split ) https://docs.python.org/2/library/string.html

Get a random element of a list : How to randomly select an item from a list?

Community
  • 1
  • 1
C.LECLERC
  • 510
  • 3
  • 12
0

First, you need to read the text file into a string; find the pattern "{([a-z|]+)}" using regex, split them by "|" to make a list as random words. It could be achieved as the following:

import re, random
seed = []
matches = re.findall('{([a-z|]+)}', open('bio.txt', 'r').read())
[seed.extend(i.split('|')) for i in matches]
input = random.choice(seed)
Quinn
  • 4,394
  • 2
  • 21
  • 19