title: how to remove non utf 8 code and save as a csv file python
body: <p>I have some amazon review data and I have converted from the text format to CSV format successfully, now the problem is when I trying to read it into a dataframe using pandas, i got error msg: 
  <em>UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf8 in position 13: invalid start byte</em></p>
  
  <p>I understand there must be some non utf-8 in the review raw data, how can I remove the non UTF-8 and save to another CSV file?</p>
  
  <p>thank you!</p>
  
  <p>EDIT1:
  Here is the code i convert to text to csv:</p>
  
  <pre><code>import csv
  import string
  INPUT_FILE_NAME = "small-movies.txt"
  OUTPUT_FILE_NAME = "small-movies1.csv"
  header = [
      "product/productId",
      "review/userId",
      "review/profileName",
      "review/helpfulness",
      "review/score",
      "review/time",
      "review/summary",
      "review/text"]
  f = open(INPUT_FILE_NAME,encoding="utf-8")
  
  outfile = open(OUTPUT_FILE_NAME,"w")
  
  outfile.write(",".join(header) + "\n")
  currentLine = []
  for line in f:
  
     line = line.strip()  
     #need to reomve the , so that the comment review text won't be in many columns
     line = line.replace(',','')
  
     if line == "":
        outfile.write(",".join(currentLine))
        outfile.write("\n")
        currentLine = []
        continue
     parts = line.split(":",1)
     currentLine.append(parts[1])
  
  if currentLine != []:
      outfile.write(",".join(currentLine))
  f.close()
  outfile.close()
  </code></pre>
  
  <h1>EDIT2:</h1>
  
  <p>Thanks to all of you trying to helping me out. 
  So I have solved it by modify the output format in my code:</p>
  
  <pre><code> outfile = open(OUTPUT_FILE_NAME,"w",encoding="utf-8")
  </code></pre>