For Loop problem in Python
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 ou开发者_JS百科tput 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.
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:
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.
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.
精彩评论