title: How to write clean data to a file in python in tabulated format
body: <p>Issue: Remove the hyperlinks, numbers and signs like <code>^&amp;*$ etc</code> from twitter text. The tweet file is in CSV tabulated format as shown below:</p>
  
  <pre><code>s.No.   username   tweetText
  
  1.      @abc  This is a test #abc example.com
  2.      @bcd  This is another test #bcd example.com 
  </code></pre>
  
  <p>Being a novice at python, I search and string together the following code, thanks to a the code given <a href="https://stackoverflow.com/questions/8376691/how-to-remove-hashtag-user-link-of-a-tweet-using-regular-expression">here</a>: </p>
  
  <pre><code>import re
  fileName="path-to-file//tweetfile.csv"
  fileout=open("Output.txt","w")
  with open(fileName,'r') as myfile:
      data=myfile.read().lower() # read the file and convert all text to lowercase
      clean_data=' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",data).split()) # regular expression to strip the html out of the text
  fileout.write(clean_data+'\n') # write the cleaned data to a file
  fileout.close()
  myfile.close()
  print "All done"
  </code></pre>
  
  <p>It does the data stripping, but the output file format is not as I desire. The output text file is in a single line like</p>
  
  <p><code>s.no username tweetText 1 abc</code> This is a cleaned tweet <code>2 bcd</code> This is another cleaned tweet <code>3 efg</code> This is yet another cleaned tweet </p>
  
  <p>How can I fix this code to give me an output like given below:</p>
  
  <pre><code>s.No. username  tweetText
  
  1  abc  This is a test
  
  2  bcd  This is another test
  
  3  efg  This is yet another test 
  </code></pre>
  
  <p>I think something needs to be added in the regular expression code but I don't know what it could be. Any pointers or suggestions will be helpful.        </p>