I have a text file that I am opening in my python code. I want to search through the file and pull out the text that is followed by a specific symbol. For instance my text file name File.txt is:
Hello, this is just a dummy file that has information with no substance at all and I want to pull the information between the dollar sign symbols. So all of this $ in between here should be pulled out so I can do what ever I want to with it $ and the rest of this will be a second group.
Here is a sample of my code:
class FileExtract(object):
__init__(self):
pass
def extractFile(self):
file = open(File.txt)
wholeFile = file.read()
file.close()
symCount = wholefile.count("$")
count = 0 #Will count the each $ as it finds it
begin = False #determines which the $ has been found and begin to start copying word
myWant = [] #will add the portion I want
for word in wholeFile.split():
while(count != symCount):
if word != "$" and begin == False:
break
if word == "$" and begin == False:
myWant.append(word)
begin = True
count = count + 1 #it found one of the total symbols
break
elif word != "$" and begin == True:
myWant.append(word)
break
elif word == "$" and begin == True:
begin = False
break
print myWant
I would like for it to print:
"$ in between here should be pulled out so I can do what ever I want to with it"
"$ and the rest of this will be a second group."
This is the only way I can think to pull the text out (which I know is horrible, please take it easy Im just learning). The problem is that my way is putting it into a list and I would like for it to just print the string out with spaces, newlines, and all. Any suggestions or other build in functions/methods that I am overlooking that would help me?