title: Python: splitlines() from text file then write them back to text file
body: <p>I am working on making a python program that fetches data from a website then records it in a text file. I want this to record the last 1000 (I am testing with 4 and a string "hello") entries and delete the rest. Here is what I have so far:</p>
  
  <pre><code>f = open("test.txt", "r")
  text = f.read()
  
  f = open("test.txt", "w")
  content = text.splitlines(True)
  f.write("hello")
  f.write("\n")
  
  for x in range(0,4):
      f.write(str(content[x:x+1]).strip('[]'))
  
  f.close()
  </code></pre>
  
  <p>This "works" however formats the text file like this:</p>
  
  <pre><code>hello
  'hello\n''\'hello\\n\'\'\\\'hello\\\\n\\\'\\\'\\\\\\\'hello\\\\\\\\n\\\\\\\'"\\\\\\\'hello\\\\\\\\\\\\\\\\n\\\\\\\'"\\\'\''
  </code></pre>
  
  <p>Can you help me figure this out so it looks like this:</p>
  
  <pre><code>hello
  hello
  hello
  hello
  </code></pre>
  
  <p>Thank you!</p>