SyntaxError: Non-ASCII character. Python

2019-09-20 02:24发布

问题:

Could somebody tell me which character is a non-ASCII character in the following:

Columns(str) – comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible columns are: id, entry name, length, organism. Some column names must be followed by a database name (i.e. ‘database(PDB)’). Again see uniprot website for more details. See also _valid_columns for the full list of column keyword.

Essentially I am defining a class and trying to give it a comment to define how it works:

def test(self,uniprot_id):
    '''
    Same as the UniProt.search() method arguments:
    search(query, frmt='tab', columns=None, include=False, sort='score', compress=False, limit=None, offset=None, maxTrials=10)


    query (str) -- query must be a valid uniprot query. See http://www.uniprot.org/help/text-search, http://www.uniprot.org/help/query-fields See also example below
    frmt (str) -- a valid format amongst html, tab, xls, asta, gff, txt, xml, rdf, list, rss. If tab or xls, you can also provide the columns argument. (default is tab)
    include (bool) -- include isoform sequences when the frmt parameter is fasta. Include description when frmt is rdf.
    sort (str) -- by score by default. Set to None to bypass this behaviour
    compress (bool) -- gzip the results
    limit (int) -- Maximum number of results to retrieve.
    offset (int) -- Offset of the first result, typically used together with the limit parameter.
    maxTrials (int) -- this request is unstable, so we may want to try several time.
    Columns(str) -- comma-seperated list of values. Works only if format is tab or xls. For UnitprotKB, some possible columns are: id, entry name, length, organism. Some column names must be followed by a database name (i.e. ‘database(PDB)’). Again see uniprot website for more details. See also _valid_columns for the full list of column keyword. '

    '''        
    u = UniProt()
    uniprot_entry = u.search(uniprot_id)
    return uniprot_entry

Without the line 52, i.e. the one beginning with 'columns' in the quoted out comment block, this works as expected but as soon as I describe what 'columns' is I get the following error:

SyntaxError: Non-ASCII character '\xe2' in file /home/cw00137/Documents/Python/Identify_gene.py on line 52, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

Does anybody know what is going on?

回答1:

You are using 'fancy' curly quotes in that line:

>>> u'‘database(PDB)’'
u'\u2018database(PDB)\u2019'

That's a U+2018 LEFT SINGLE QUOTATION MARK at the start and U+2019 RIGHT SINGLE QUOTATION MARK at the end.

Use ASCII quotes (U+0027 APOSTROPHE or U+0022 QUOTATION MARK) or declare an encoding other than ASCII for your source.

You are also using an U+2013 EN DASH:

>>> u'Columns(str) –'
u'Columns(str) \u2013'

Replace that with a U+002D HYPHEN-MINUS.

All three characters encode to UTF-8 with a leading E2 byte:

>>> u'\u2013 \u2018 \u2019'.encode('utf8')
'\xe2\x80\x93 \xe2\x80\x98 \xe2\x80\x99'

which you then see reflected in the SyntaxError exception message.

You may want to avoid using these characters in the first place. It could be that your OS is replacing these as you type, or you are using a word processor instead of a plain text editor to write your code and it is replacing these for you. You probably want to switch that feature off.



回答2:

Previously encountered the same problem and same error, python2 will default to using ASCII encoding. You can try to declare the following comment in the py file's first or second line:

# -*- coding: utf-8 -*-