Hi all,
I'm a completely new user of python (and by new I mean I started using it yesterday) but I've been programming for a couple of years now in other languages such as Java. I have a question to ask you all.
So what I want to do is read through a text file containing code and extract only the comments from the document (.txt file). I'll then take those comments and put them in another text file. The comments look something like this:
//*************
Some details about the script / method
//*************
I've figured out how to find each line containing //*** (which I've used for my search query) but I'm not sure how to go about getting the text between the two lines. Please see my code below. I don't know if using readlines() along with lists is the proper way to go about doing this.
fileName = raw_input("Enter the file name you want to read (name.txt): ")
file1 = open(fileName, "r")
print "Name of file: ", file1.name
print "Reading file..."
L = file1.readlines()
comments = []
i = -1
for line in L:
if "//***" in line:
i = L.index(line, i+1)
print "Comment line found at: ", i
comments.append(i)
As you can see I'm trying to keep track of the lines in another list but it's turning out to be kinda difficult to go back and then extract the text itself. Should I keep going with this method or is there an easier way? Any help would be greatly appreciated. Thanks.