<p>The last line indicates that this is an encoding problem. Try to see if the function you are using to read the data has an "encoding" parameter that you can set to whatever encoding your file is using. If that doesn't work, you can change the encoding of your file by:</p>

<ul>
<li>with <code>save as</code> using a text editor of your choice and selecting "UTF8" as the encoding. That would be a quick work-around if you have a small number of files.</li>
</ul>

<p>or</p>

<ul>
<li>You can use the <code>codecs</code> library to check and convert file encodings within your code. <a href="https://stackoverflow.com/a/191403/7236472">Here is an example</a>.</li>
</ul>

<p>Warning: I haven't used <code>Biopython</code> in ages but encountered similar problems with Python in the past.</p>

CC BY-SA 4.0
2019-12-31T10:44:27.967Z
2019-12-31T10:44:27.967Z
haci
<p>I have a file in Stockholm format and I am trying to read it using the Biopython package. The code is as follows :</p>

<pre><code>import numpy as np
from Bio import AlignIO

alignments = AlignIO.parse("Rfam.seed", "stockholm")
for alignment in alignments:
    print(alignment)
</code></pre>

<p>It gets read for some alignments and then throws up following error :</p>

<pre><code>Traceback (most recent call last):
  File "test.py", line 5, in &lt;module&gt;
    for alignment in alignments:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Bio/AlignIO/__init__.py", line 394, in parse
    for a in i:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Bio/AlignIO/StockholmIO.py", line 408, in __next__
    line = handle.readline()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 2468: invalid continuation byte
</code></pre>

<p>How to resolve this error?</p>

CC BY-SA 4.0
2019-12-31T10:44:10.060Z
Can you not just remove the unicode character from your file? The error is due to a Unicode character (å)...
Matteo Ferla
<python>
Unable to read all alignments in stockholm format : UnicodeDecodeError: 'utf-8'
# Unable to read all alignments in stockholm format : UnicodeDecodeError: 'utf-8'

(Asked by: username_0 on 2019-12-30)

I have a file in Stockholm format and I am trying to read it using the Biopython package. The code is as follows :
import numpy as np
from Bio import AlignIO

alignments = AlignIO.parse("Rfam.seed", "stockholm")
for alignment in alignments:
    print(alignment)

It gets read for some alignments and then throws up following error :
Traceback (most recent call last):
  File "test.py", line 5, in 
    for alignment in alignments:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Bio/AlignIO/__init__.py", line 394, in parse
    for a in i:
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/Bio/AlignIO/StockholmIO.py", line 408, in __next__
    line = handle.readline()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 2468: invalid continuation byte

How to resolve this error?

**Tags:** python
> **Comments:**
> **username_1**: Can you not just remove the unicode character from your file? The error is due to a Unicode character (å)...  

---

## 1 answers

### Answer by username_2 on 2019-12-31. Score: 2 | Accepted answer

The last line indicates that this is an encoding problem. Try to see if the function you are using to read the data has an "encoding" parameter that you can set to whatever encoding your file is using. If that doesn't work, you can change the encoding of your file by:

* with `save as` using a text editor of your choice and selecting "UTF8" as the encoding. That would be a quick work-around if you have a small number of files.

or

* You can use the `codecs` library to check and convert file encodings within your code. [Here is an example](https://stackoverflow.com/a/191403/7236472).

Warning: I haven't used `Biopython` in ages but encountered similar problems with Python in the past.


---

