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()

Try using readlines instead, and check that searchWord actually contains something otherwise you can write blanks to the file.

 with open("SearchText.txt").readlines() as records:
     for num, line in enumerate(records):
 ......
         ## the following line will yield an error on the
         ## last record because there is no num+1
         response=records[num +1]

I don't understand what "stuck in a loop" means. The only loop(s) are the two for() loops and they exit when the data is exhausted.

## with open("SearchText.txt").readlines() as test_records:
test_records =["JOHN",
               "EGGS,24,BEEF,36,BACON,56,HAM,66",
               "ALICE",
               "TOMATOES,16,HAM,35,BEANS,35",
               "PAT",
               "APPLES,20,ORANGES,30,LETTUCE,50"]

response = 2
## response item location 1-->0, 2-->2, 3-->4 == response *2 -2
item_location = response*2-2
for ctr in range(0, len(test_records), 2):
    name = test_records[ctr]
    items = test_records[ctr+1].split(',')
    print items[item_location],
print 

Hi. Thanks. My problem is the end of the program. The second if statement is inside the for loop.

 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 + " ")

I just can't work out

if(not in search):
Print DumpedWords

Thanks.

You can use and else. What is wrong with the code I posted earlier?

        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
        else:
               DumpText.write(j + " ")

H. I tried this(simplified program)

import linecache, re
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('Norm_text.txt')
list1 = indWords
list1 = [element.upper() for element in list1]
for i, search_word in enumerate(list1):

  item_location = int(respNum)*2-2

  with open("db_text.txt").readlines() as test_records:
      for ctr in range(0, len(test_records), 2):
        search_word = item_location[ctr]
        items = item_location[ctr+1].split(',')
        print items[item_location],

This gives me:
with open("db_text.txt").readlines() as test_records:
AttributeError: exit
?? Thanks

else:
               DumpText.write(j + " ")

This prints the words many times as I believe it's printing the input word every search line. I need for it to complete to the end and then store the word not in the search file.

I am not sure what you want to do, print names not found or print the food associated with the name. This prints the names not found.

##------------------------------------------------------------------
## names_file = open("file_name", "r").readlines()
names_file = """John
Alice
Mary""".split("\n")

## strip white space and capitalize
names_list = []
for name in names_file:
    add_name = name.strip().upper()
    names_list.append(add_name)
print "names_list", names_list, "\n"

##------------------------------------------------------------------
## data_file = open("this_file_name", "r").readlines()
data_file = """JOHN
EGGS,24,BEEF,36,BACON,56,HAM,66
ALICE
TOMATOES,16,HAM,35,BEANS,35
PAT
APPLES,20,ORANGES,30,LETTUCE,50
Mary
Yogurt,21,Milk,31,Cream,41
George
Beans,22,Peas,23,Onions,33""".split("\n")
for ctr in range(0, len(data_file), 2):
    this_record = data_file[ctr]
    if this_record.strip().upper() not in names_list:
        print this_record, "***NOT Found***", data_file[ctr+1]
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.