0

I am very new to programming. I want to be able to run this program for both entries in my list but do not understand programming enough to get it right. Members of this site have helped me get this far, and the program works great for one set of tags. I want it to run so I can search multiple authors in one pass. The output is that it only gathers all the information for "Shakes." and then stops. I just do not know how to format the loops to make it right.

Here is the code I am working with:

authorList = ['Shakes.','Scott']

with open('/Users/Adam/Desktop/Poetrylist.txt','w') as output_file:
with open('/Users/Adam/Desktop/2e.txt','r') as open_file:
        the_whole_file = open_file.read()
        start_position = 0
        for x in authorList:
            while True:
               start_position = the_whole_file.find('<A>'+x+'</A>', start_position)
               if start_position < 0:
                   break
               end_position = the_whole_file.find('</W>', start_position)
               output_file.write(the_whole_file[start_position:end_position+4])
               output_file.write("\n")    
               start_position = end_position + 4

I am sure this is a very simple problem, but I am trying to teach myself python and it is going quite slowly.

2
  • What does your file format look like? Perhaps we can help if we know what we're trying to analyze. Commented Jul 22, 2011 at 21:06
  • The file is very much like an xml file. I am searching for occurences of "<A>Shakes.<A>" and "<A>Scott</A>" and then writing to a file all of the information after those tags that end with </w>. The code works great for one author, but I have 179 authors to search and I would like to create a list, have the program search for all the "<A>Shakes.</A>" then search for all the "<A>Scott</A>" and so on. I just started programming a week ago and I am trying to learn as much as I can, I just don't know where the for loop should be or even if that is the right kind of loop to use. Commented Jul 22, 2011 at 21:11

3 Answers 3

5

I think this is because you are not resetting start_position; variables in Python are scoped to their enclosing function or class or module, but not to their enclosing loop. So try adding this line:

for x in authorList:
    start_position = 0   # Add this
    while True:
Sign up to request clarification or add additional context in comments.

1 Comment

cdhowie, thank-you that makes complete sense. I knew it was something very simple but I am learning as I go.
2

This will be a problem

    start_position = 0
    for x in authorList:
        while True:

For the first author, start_position is 0. Cool.

For the second author, what will start_position be? Hint. Nothing resets it to zero.

1 Comment

Thanks for the help! Now that I see it, it makes complete sense. Much appreciated. I promise when I actually know anything I will contribute to this board. Everyone here has been so helpful.
1

This processes the input file with one pass:

#!/usr/bin/env python

authorList = ['Shakes.','Scott']
with open('/Users/Adam/Desktop/2e.txt','r') as open_file:
    the_whole_file = open_file.read()
    start_position = 0
    for x in authorList:
        while True:
           start_position = the_whole_file.find('<A>')
           if start_position < 0:
               break
           author_end = the_whole_file.find('</A>', start_position)
           end_position = the_whole_file.find('</W>', start_position)
           if the_whole_file[start+position + 3:author_end] in authorList:
               output_file.write(the_whole_file[start_position:end_position+4])
               output_file.write("\n")    
           start_position = end_position + 4

It inverts the loop by scanning for author tags, seeing if their value is in your list of authors, and printing until the closing tag if so. This may or may not be less efficient than looping over each of the authors as it has to visit each tag.

1 Comment

Thank-you Kirk. This is very helpful as well. Much appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.