title: create dictionary, keys assigned from a list, values extracted from block of text
body: <p>I've cobbled this code together over the last week- I've learned a lot but a lot of these concepts are still hard for me to wrap my head around.  I would appreciate any and all help even if its just pointing to the right direction to look.   Thanks in advance for reading...</p>
  
  <p>I get an email with abuse information that needs to be sent out every day.  The pertinent information for each abuse report is dilineated by a line of dashes:</p>
  
  <p>"----------------------------------------------------------------------"</p>
  
  <p>C9/Attacker IP: 9.48.9[.]969</p>
  
  <p>C9 Domain: </p>
  
  <p>Abuse Address: abuse@9.com | </p>
  
  <p>Notice: FIRST</p>
  
  <p>C9 Port(s): 93 | </p>
  
  <p>C9 Active on Our System: 9 Day</p>
  
  <p>Malware Family: gafgyt</p>
  
  <p>Attacks in Last 94 Hours: 9</p>
  
  <p>Attack Samples: </p>
  
  <pre><code>Attack Time: 9098-06-99T99:99:00.000Z
  
      - Victim: 9.59.998[.]999
  
      - Attack Type: udp
  
      - Attack Duration: 300 seconds
  
      - Attack Port: 8080
  </code></pre>
  
  <p>"----------------------------------------------------------------------"</p>
  
  <p>C9/Attacker IP: 9.9.69[.]39</p>
  
  <p>C9 Domain: </p>
  
  <p>Abuse Address: abuse@9.it | </p>
  
  <p>Notice: FIRST</p>
  
  <p>C9 Port(s): 666 | </p>
  
  <p>C9 Active on Our System: 9 Day</p>
  
  <p>Malware Family: mirai</p>
  
  <p>Attacks in Last 94 Hours: 9</p>
  
  <p>Attack Samples: </p>
  
  <pre><code>Attack Time: 9098-06-99T99:99:08.000Z
  
      - Victim: 9.9.948[.]98
  
      - Attack Type: udp_plain
  
      - Attack Duration: 300 seconds
  
      - Attack Port: None
  </code></pre>
  
  <p>"----------------------------------------------------------------------"</p>
  
  <p>There can be any number of abuse reports that need to be sent, there is not a fixed number.  </p>
  
  <p>Plan:
  1. note line number of dashes:
  2. copy lines between dashes into list
  3. create dictionary, key ->index of dash lines, value -> list of lines between the dashes</p>
  
  <pre><code>from itertools import islice
  from collections import defaultdict
  #dashes separate each abuse report in file test2.txt
  dashes = "----------------------------------------------------------------------"
  #create empty list dash_lines of line #s having dashes
  dash_lines = []
  #index of which "dash line" to start copying data
  index = 0
  #index2 tells where to stop (in this case, the next line of dashes found in the document)
  index2 = 1
  #holds the c2 abuse info, ideally i would want multiple buckets for each abuse report / email to be sent or to have an abuse email sent and corresponding bucket emptied before adding to it again
  c2_list = []
  #c2_bucket = {}
  c2_bucket = defaultdict(list)
  </code></pre>
  
  <ol>
  <li>Note Line Number of dashes:</li>
  </ol>
  
  <p>This creates a list of line numbers containing dashes from test2.txt </p>
  
  <pre><code>def find_dash_lines():                                                                      
      with open ("test2.txt", 'rt') as c2_email_body:                 #opens (then closes whend done) test2.txt containing abuse info
          for num, line in enumerate(c2_email_body):                  #line count in test2.txt
              if dashes in line:                                      #if dashes are found:
                  dash_lines.append(num)                              #append line number to dash_lines list
  #               print("Found A dash at line:", num)                 #error testing
          if not dash_lines:                                          #if there's not dash lines in list give error
              print("Could not determine dash line locations")        #to let me know there's an error
          else:
              dash_lines.append(1000)                                 #append line #1000 to end of list to ensure data is captured since there is no trailing dash line for last abuse report
              for num in dash_lines:
                  print("Found A dash at line:", num)                 #just to ensure something is happening will eventually comment out
  find_dash_lines()
  </code></pre>
  
  <ol start="2">
  <li><p>Extract abuse report between dashes.
  This snip of code is able to extract or print the first abuse report between dashes in test.txt</p>
  
  <p>def c2_list_function():<br>
      with open ("test2.txt", 'rt') as c2_email_body:                                                                 #opens (then closes whend done) test2.txt containing abuse info<br>
          index = 0<br>
          index2 = 1<br>
          for line in islice(c2_email_body, dash_lines[index], dash_lines[index2]):                                   #looks at line #s in the document from dash_lines[0] to dash_lines[1]- from the first line of dashes found to the next<br>
              line = line.replace("]","")                                                                             #cleanup<br>
              line = line.replace("[","")<br>
              line = line.replace("|",";")<br>
              c2_list.append(line)<br>
              index += 1                                                                                              #increases each index (used to determine where to slice)<br>
              index2 += 1                                                                                             #increases each index (used to determine where to slice)  </p></li>
  <li><p>create a dictionary. 
  key should be line number / index from dash_lines
  val should be list of lines from c2_list, each block of text between dash lines</p>
  
  <p>def c2_dict_function():<br>
      for x in range(len(dash_lines)):<br>
          c2_list_function()<br>
          for line in c2_list:<br>
              print(line, end="")<br>
          c2_bucket[x].append(c2_list)<br>
          c2_list.clear()  </p></li>
  </ol>
  
  <p>The dictionary populates the keys but the values are empty. example output: </p>
  
  <pre><code>dict_keys([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16])
  dict_values([[[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]], [[]]])
  </code></pre>
  
  <p>the c2_list also does not seem to populate correctly it looks like it only grabs the first group of info between dashes - i'm assuming i'm not doing the index progression correctly.</p>