I am a newbie to python so exude the not so well constructed code.. I have two text files.
The first one (WordFile1.txt) is " John Mary Joe alice William ….."
The second (SearchText.txt) is
"JOHN
EGGS,24,BEEF,36,BACON,56,HAM,66
ALICE
TOMATOES,16,HAM,35,BEANS,35"
……."
If my response number is 2 I will get BEEF and HAM stored… no problem.
I want to store in the names NOT in the SearchText.txt (Mary Joe William) to Dumptext.txt but my second "if" statement is stuck in the loop.
Please help. Thanks for supporting newbies!
#
respNum = raw_input("What is the response number: " ,)
def read_words(words_file):
with open(words_file, 'r') as f:
ret = []
for line in f:
ret += line.split()
return ret
indWords = read_words('WordFile1.txt')
Firstload = open("WordFile1.txt", "w")
DumpText = open("Dumptext.txt", "w")
DumpText.write("\n\n")
DumpText.write("This is the rejected words on first pass\n")
DumpText.close()
DumpText = open("Dumptext.txt", "a")
list1 = indWords
list1 = [element.upper() for element in list1]
for i,j in enumerate(list1):
respNum2 = int(respNum)
NumList = [0,0,2,4,6,8,10,12,14,16,18,20]
z = NumList [respNum2]
with open("SearchText.txt") as myFile:
for num, line in enumerate(myFile, 1):
if j +"\n" == line and num %2 == 1:
response= linecache.getline ("SearchText.txt", num +1)
wordList = re.sub("[^\w]", " ", response).split()
searchWord = wordList [z] + " "
Firstload.write(searchWord)
# PROBLEM HERE
if j +"\n" != line and num %2 != 1:
DumpText.write(j + " ")
Firstload.close()
DumpText.close()