We are going to look closer at how to parse text and look for the information you want after you download/open a file. Often, text is messy - it isn’t nicely laid out like a CSV file where each data point is separated cleanly from the next. Sometimes you have to figure out ways to hunt through a lot of information to pull out just the one nugget you want.
Let’s look through the string API to see what we can find!
# Mark Sherriff (mss2x)speaking=""count=0line_number=1debate_file=open("2012debate.txt","r")forlineindebate_file:line=line.strip()ifline.startswith("SCHIEFFER"):speaking="SCHIEFFER"elifline.startswith("OBAMA"):speaking="OBAMA"elifline.startswith("ROMNEY"):speaking="ROMNEY"if"America"inline:words=line.split(" ")forwordinwords:if"America"inword:print(speaking,"said America on line",line_number)count+=1line_number+=1print(count)