title: Python: Multiple Text Files to Dataframe
body: <p>I'm a little stuck on how exactly to proceed, so a little nudge would be very helpful.</p>
  
  <p>I have ~1800 text files, emails actually, that are in a <strong>repeated format</strong>.</p>
  
  <p>The structure of each file is as follows:</p>
  
  <pre><code>From: Person-1 [email@person-1.com]
  Sent: Tuesday, April 18, 2017 11:24 AM
  To: email@person-2.com
  Subject: Important Subject
  
  User, 
  
  Below is your search alert.
  
  Target: text
  
  Attribute: text
  
  Label: abcdef
  
  Time: Apr 18, 2017 11:24 EDT
  
  Full Text: Text of various length exists here. Some files even have links. I'm not sure how I would capture a varied length field.
  
  Recording: abcde &amp; fghijk lmnop
  </code></pre>
  
  <p>That's the gist of it. </p>
  
  <p>I would like to write that into a DF I can store as a CSV.</p>
  
  <p>I would like to end up with maybe something like this?</p>
  
  <pre><code>| Target | Attribute |  Label  |  Time  |  Full Text  | Recording | Filename |
  |--------|-----------|---------|--------|-------------|-----------|----------|
  |    text|       text|   abcdef| (date) |(Full text..)|abcde &amp; f..| 1111.txt |
  |   text2|      text2|  abcdef2| (date) |(Full text..)|abcde &amp; f..| 1112.txt |
  </code></pre>
  
  <p>Where the 2nd row is another text file.</p>
  
  <p>I have code to go through all of the text files and print them. Here's that code:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  import os
  import sys
  
  # Take all text files in workingDirectory and put them into a DF.
  def convertText(workingDirectory, outputDirectory):
      if workingDirectory == "": workingDirectory = os.getcwd() + "\\" # Returns current working directory, if workingDirectory is empty.
      i = 0
      for txt in os.listdir(workingDirectory): # Iterate through text filess in workingDirectory
          print("Processing File: " + str(txt))
          fileExtension = txt.split(".")[-1]
          if fileExtension == "txt":
              textFilename = workingDirectory + txt # Becomes: \PATH\example.text
              f = open(textFilename,"r")
              data = f.read() # read what is inside
              print data # print to show it is readable
  
              #RegEx goes here?
  
              i += 1 # counter
      print("Successfully read " + str(i) + " files.")
  
  
  def main(argv):
      workingDirectory = "../Documents/folder//" # Put your source directory of text files here
      outputDirectory = "../Documents//" # Where you want your converted files to go.
  
      convertText(workingDirectory, outputDirectory)
  
  if __name__ == "__main__":
      main(sys.argv[1:])
  </code></pre>
  
  <p>I guess I would need RegEx, maybe, to parse the files? What would you recommend?</p>
  
  <p>I am not opposed to using R or something else, if it makes more sense.</p>
  
  <p>Thank You.</p>