Python script won't handle non-ASCII characters

Viewed 45

I'm trying to do some machine learning learning on a sequencing dataset that involves special characters and Greek alphabet. and it keeps throwing errors. Every single time I run the script it throws an error from different position.

file = open(filename,encoding="utf-8")
for record in SeqIO.parse(file,"fasta"):
        print("ID %s" % record.id)
        print("Sequence length %i" % len(record))

UnicodeEncodeError: 'ascii' codec can't encode character '\u0394' in position 2635: ordinal not in range(128)

1 Answers

line 1725 Bio/Seq.py

....
   self._data = self._data = bytes(data, encoding="ASCII")
....
                

giving:

data ='\u0394'

data = bytes(data, encoding="ASCII")

print(data.decode())

output:

UnicodeEncodeError: 'ascii' codec can't encode character '\u0394' in position 0: ordinal not in range(128)

else:

data ='\u0394'

data = bytes(data, encoding="utf-8")

print(data.decode())

output:

Δ

I guess

library doesn't support non-ASCII characters

for the sequences as pointed out by @gre_gor

Related