title: Python: UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
body: <p>I'm currently have an issue with my python 3 code. </p>
  
  <pre><code> replace_line('Products.txt', line, tenminus_str)
  </code></pre>
  
  <p>Is the line I'm trying to turn into utf-8, however when I try to do this like I would with others, I get errors such as no attribute ones and when I try to add, for example...</p>
  
  <pre><code>.decode("utf8")
  </code></pre>
  
  <p>...to the end of it, I still get errors that it is using ascii. I also tried other methods that worked with other lines such as adding io. infront and adding a comma with </p>
  
  <pre><code>encoding = 'utf8'
  </code></pre>
  
  <p>The function that I am using for replace_line is:</p>
  
  <pre><code>def replace_line(file_name, line_num, text):
      lines = open(file_name, 'r').readlines()
      lines[line_num] = text
      out = open(file_name, 'w')
      out.writelines(lines)
      out.close()
  </code></pre>
  
  <p>How would I fix this issue? Please note that I'm very new to Python and not advanced enough to do debugging well.</p>
  
  <p>EDIT: Different fix to this question than 'duplicate'</p>
  
  <p>EDIT 2:I have another error with the function now. </p>
  
  <pre><code>File "FILELOCATION", line 45, in refill replace_line('Products.txt', str(line), tenminus_str) 
  
  File "FILELOCATION", line 6, in replace_line lines[line_num] = text
  
  TypeError: list indices must be integers, not str 
  </code></pre>
  
  <p>What does this mean and how do I fix it?</p>