title: How to automatically call for a next iterator inside a loop
body: <p>I recently started scripting and am having difficulties with nested loops. I'm not getting the first iterator object from the first loop as an input to the second to run properly.</p>
  
  <p>The problem itself is quite simple. I would like to change the second item (‘20’) on row 1 in my data to a number from the range and create a file.
  So if the first number from the range is 14 then the first line of the file is (L,14,0,0,0,0) and gets a name data1.txt.</p>
  
  <p><strong>Data:</strong></p>
  
  <p>L,1,5.827,20,-4.705,0<br>
  L,20,0,0,0,0<br>
  L,12,15,0,-6,0</p>
  
  <p><strong>Original Script:</strong></p>
  
  <pre><code>import re
  from itertools import islice
  import numpy as np
  
  x = np.arange(14,30.5,0.5)  
  size = x.size                            
  
  with open('data.txt', 'r') as line:                              
      for line in islice(line, 1, 2):
          re.sub(r'\s', '', line).split(',')          
      nline = line[:2] + line[3:]
  
  x = iter(x)
  y = next(x)
  
  for i in x:
      nline = nline[:2] + str(y)+ nline[3:]           
  
  with open('data.txt', 'r') as file:
      data = file.readlines()
      data[1] = nline
  
  for i in range(1,size):                                 
      with open('data%i.txt' %i, 'w') as file:          
          file.writelines(data) 
  </code></pre>
  
  <p>EDITED:</p>
  
  <p>Ive made some progress with my script and Im almost there. </p>
  
  <p>After the first loop I have the output that I need (33 cases). All I would like to do now is to write them to a 33 unique files, named data1 to data33. What seems to happen however is that the second loop iterates through the first loop another 33 times and creates 1089 cases. So what ends up in the files is only the last line of the first loop. </p>
  
  <p>Any suggestions how to allow the second loop for file creation but disable it for data?  </p>
  
  <p><strong>Updated Script:</strong></p>
  
  <pre><code>import re
  from itertools import islice
  import numpy as np
  
  x = np.arange(14,30.5,0.5)  
  size = x.size                            
  
  with open('data.txt', 'r') as line:                              
     for line in islice(line, 1, 2):
         re.sub(r'\s', '', line).split(',')
  
  for i in x:
      y=str(i)                                              
      nline = line[:2] + y + line[4:]
  
      with open('data.txt', 'r') as file:
          data = file.readlines()
          data[1] = nline
  
          for i in range(1,size+1):    
              with open('data%i.txt' %i, 'w') as file:
                  file.writelines(data)
                  print data
  </code></pre>