Python 3 Extracting Candidate Words from a Debate File
<p>This is my first post, so I'm sorry if I do anything wrong. That said, I searched for the question and found something similar that was never answered due to the OP not giving sufficient information. This is also homework, so I'm just looking for a hint. I really want to get this on my own.</p>

<p>I need to read in a debate file (.txt), and pull and store all of the lines that one candidate says to put in a word cloud. The file format is supposed to help, but I'm blanking on how to do this. The hint is that each time a new person speaks, their name followed by a colon is the first word in the first line. However, candidates' data can span multiple lines. I am supposed to store each person's lines separately. Here is a sample of the file:</p>

<blockquote>
  <p>LEHRER: This debate and the next three -- two presidential, one vice
  presidential -- are sponsored by the Commission on Presidential
  Debates. Tonight's 90 minutes will be about domestic issues and will
  follow a format designed by the commission. There will be six roughly
  15-minute segments with two-minute answers for the first question,
  then open discussion for the remainder of each segment.</p>
  
  <p>Gentlemen, welcome to you both. Let's start the economy, segment one,
  and let's begin with jobs. What are the major differences between the
  two of you about how you would go about creating new jobs?</p>
  
  <p>LEHRER: You have two minutes. Each of you have two minutes to start. A
  coin toss has determined, Mr. President, you go first.</p>
  
  <p>OBAMA: Well, thank you very much, Jim, for this opportunity. I want to
  thank Governor Romney and the University of Denver for your
  hospitality.</p>
  
  <p>There are a lot of points I want to make tonight, but the most
  important one is that 20 years ago I became the luckiest man on Earth
  because Michelle Obama agreed to marry me.</p>
</blockquote>

<p>This is what I have for a function so far:</p>

<pre><code>def getCandidate(myFile):    
    file = open(myFile, "r")
    obama = []
    romney = []
    lehrer = []
    file = file.readlines()
</code></pre>

<p>I'm just not sure how to iterate through the data so that it separates each person's words correctly. I created a dummy file to create the word cloud, and I'm able to do that fine, so all I am wondering is how to extract the information I need.</p>

<p>Thank you! If there is more information I can offer please let me know. This is a beginning Python course.</p>

<p>EDIT: New code added from a response. This works to an extent, but only grabs the first line of each candidate's response, not their entire response. I need to write code that continues to store each line under that candidate until a new name is at the start of a line.</p>

<pre><code>def getCandidate(myFile, candidate):   
    file = open(myFile, "r")
    OBAMA = []
    ROMNEY = []
    LEHRER = []
    file = file.readlines()
    for line in file:
        if line.startswith("OBAMA:"):
            OBAMA.append(line)
        if line.startswith("ROMNEY:"):
            ROMNEY.append(line)
        if line.startswith("LEHRER:"):
            LEHRER.append(line)
    if candidate == "OBAMA":
        return OBAMA
    if candidate == "ROMNEY":
        return ROMNEY
</code></pre>

<p>EDIT: I now have a new question. How can I generalize the file so that I can open any debate file between two people and a moderator? I am having a lot of trouble with this one.</p>

<p>I've been given a hint to look at the beginning of the line and see if the last word of each line to see if it ends in ":", but I'm still not sure how to do this. I tried splitting each line on spaces and then looking at the first item in the line, but that's as far as I've gotten.</p>
