Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

subtraction is giving me a syntax error

11 views
Skip to first unread message

Joel Pendery

unread,
Mar 15, 2010, 1:37:34 PM3/15/10
to
So I am trying to write a bit of code and a simple numerical
subtraction

y_diff = y_diff-H

is giving me the error

Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
encoding declared.

Even though I have deleted some lines before it and this line is no
longer line 70, I am still getting the error every time. I have tried
to change the encoding of the file to utf-8 but to no avail, I still
am having this issue. Any ideas?

Thanks in advance

Richard Brodie

unread,
Mar 15, 2010, 1:42:50 PM3/15/10
to

"Joel Pendery" <joel.p...@gmail.com> wrote in message
news:56597268-3472-4fd9...@e7g2000yqf.googlegroups.com...

>> y_diff = y_diff-H


>
> Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
> encoding declared.

That's likely an en-dash, not a minus sign.


Philip Semanchuk

unread,
Mar 15, 2010, 1:48:54 PM3/15/10
to Python (General)


0x96 is the Win1252 minus sign character. Did you copy & paste this
code from a Web browser or a word processor?

Delete the character between "y_diff" and "H" and replace it with a
plain ASCII subtraction sign.

MRAB

unread,
Mar 15, 2010, 2:00:42 PM3/15/10
to pytho...@python.org

Character '\x96' is an en-dash, which looks like a hyphen/minus sign
'-', but isn't.

Grant Edwards

unread,
Mar 15, 2010, 2:09:29 PM3/15/10
to
On 2010-03-15, Philip Semanchuk <phi...@semanchuk.com> wrote:
> On Mar 15, 2010, at 1:37 PM, Joel Pendery wrote:
>
>> So I am trying to write a bit of code and a simple numerical
>> subtraction
>>
>> y_diff = y_diff-H
>>
>> is giving me the error
>>
>> Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
>> encoding declared.
[...]

>
> 0x96 is the Win1252 minus sign character. Did you copy & paste this
> code from a Web browser or a word processor?
>
> Delete the character between "y_diff" and "H" and replace it with a
> plain ASCII subtraction sign.

I think somebody needs to stop editing his code with MS Word and start
using a programming editor. ;)

--
Grant Edwards grant.b.edwards Yow! Make me look like
at LINDA RONSTADT again!!
gmail.com

Baptiste Carvello

unread,
Mar 15, 2010, 2:43:53 PM3/15/10
to pytho...@python.org
Joel Pendery a écrit :

Hello,

I would say that when you press the minus key, your operating system doesn't
encode the standard (ASCII) minus character, but some fancy character, which
Python cannot interpret.

More precisely, I suspect you are unsing Windows with codepage 1252 (latin 1).
With this encoding, you have 2 kinds of minus signs: the standard (45th
character, in hex '\x2d') and the non-standard (150th character, in hex '\x96').

cf:
http://msdn.microsoft.com/en-us/library/cc195054.aspx

Cheers,
Baptiste

Steven D'Aprano

unread,
Mar 15, 2010, 6:33:33 PM3/15/10
to
On Mon, 15 Mar 2010 18:09:29 +0000, Grant Edwards wrote:

>> Delete the character between "y_diff" and "H" and replace it with a
>> plain ASCII subtraction sign.
>
> I think somebody needs to stop editing his code with MS Word and start
> using a programming editor. ;)


I've had this error myself, and I've never used Word to edit code. It can
happen if you copy code from a website that "helpfully" converts hyphens
to en-dashes, spaces to non-breaking spaces, or inserts ctrl-Z characters
into strings, etc. They're a devil to debug.


--
Steven

John Machin

unread,
Mar 15, 2010, 6:48:45 PM3/15/10
to
On Mar 16, 5:43 am, Baptiste Carvello <baptiste...@free.fr> wrote:
> Joel Pendery a écrit :

> > So I am trying to write a bit of code and a simple numerical
> > subtraction
>
> > y_diff = y_diff-H
>
> > is giving me the error
>
> > Syntaxerror: Non-ASCII character '\x96' in file on line 70, but no
> > encoding declared.
>
>

> I would say that when you press the minus key, your operating system doesn't
> encode the standard (ASCII) minus character, but some fancy character, which
> Python cannot interpret.

The likelihood that any operating system however brain-damaged and in
whatever locale would provide by default a "keyboard" or "input
method" that generated EN DASH when the '-' key is struck is somewhere
between zero and epsilon.

Already advanced theories like "used a word processor instead of a
programmer's editor" and "scraped it off the web" are much more
plausible.

> More precisely, I suspect you are unsing Windows with codepage 1252 (latin 1).

Codepage 1252 is not "latin1" in the generally accepted meaning of
"latin1" i.e. ISO-8859-1. It is a superset. MS in their wisdom or
otherwise chose to use most of the otherwise absolutely wasted slots
assigned to "C1 control characters" in latin1.

> With this encoding, you have 2 kinds of minus signs: the standard (45th
> character, in hex '\x2d') and the non-standard (150th character, in hex '\x96').
>
> cf:http://msdn.microsoft.com/en-us/library/cc195054.aspx

The above link quite correctly says that '\x96` maps to U+2013 EN
DASH. EN DASH is not any kind of minus sign.

Aside: the syndrome causing the problem is apparent with cp125x for x
in range(9)

Grant Edwards

unread,
Mar 15, 2010, 6:56:19 PM3/15/10
to

Though it may not be Microsoft Word, I think I'd still maintain that
an editor where you can't see a ctrl-Z or tell the difference between
an ASCII minus and a windows-codepage-whatever isn't a very good
programmer's editor.

--
Grant Edwards grant.b.edwards Yow! HUMAN REPLICAS are
at inserted into VATS of
gmail.com NUTRITIONAL YEAST ...

Steven D'Aprano

unread,
Mar 16, 2010, 3:47:45 AM3/16/10
to
On Mon, 15 Mar 2010 22:56:19 +0000, Grant Edwards wrote:

> On 2010-03-15, Steven D'Aprano <st...@REMOVE-THIS-cybersource.com.au>
> wrote:
>> On Mon, 15 Mar 2010 18:09:29 +0000, Grant Edwards wrote:
>>
>>>> Delete the character between "y_diff" and "H" and replace it with a
>>>> plain ASCII subtraction sign.
>>>
>>> I think somebody needs to stop editing his code with MS Word and start
>>> using a programming editor. ;)
>>
>> I've had this error myself, and I've never used Word to edit code. It
>> can happen if you copy code from a website that "helpfully" converts
>> hyphens to en-dashes, spaces to non-breaking spaces, or inserts ctrl-Z
>> characters into strings, etc. They're a devil to debug.
>
> Though it may not be Microsoft Word, I think I'd still maintain that an
> editor where you can't see a ctrl-Z or tell the difference between an
> ASCII minus and a windows-codepage-whatever isn't a very good
> programmer's editor.

Regarding ctrl-Z (or for that matter other control characters), I agree
it's a lack.

As for not being able to see the difference between a hyphen and an EN-
dash, or minus sign, or whatever it is, yes but they are very similar
looking glyphs in virtually ever modern font. It would take a good eye to
see the difference between (say) ‐ ‑ and -.


--
Steven

Grant Edwards

unread,
Mar 16, 2010, 10:20:26 AM3/16/10
to
On 2010-03-16, Steven D'Aprano <ste...@REMOVE.THIS.cybersource.com.au> wrote:

>> Though it may not be Microsoft Word, I think I'd still maintain that
>> an editor where you can't see a ctrl-Z or tell the difference between
>> an ASCII minus and a windows-codepage-whatever isn't a very good
>> programmer's editor.
>
> Regarding ctrl-Z (or for that matter other control characters), I
> agree it's a lack.
>
> As for not being able to see the difference between a hyphen and an
> EN- dash, or minus sign, or whatever it is, yes but they are very
> similar looking glyphs in virtually ever modern font. It would take a

> good eye to see the difference between (say) ??? ??? and -.

My point is that if it's an ASCII file, then rendering \x96 as an
em-dash isn't correct. I'd expect it to show up highlighted somehow
in hex or octal so that I know it's a non-ASCII character.

--
Grant Edwards grant.b.edwards Yow! We are now enjoying
at total mutual interaction in
gmail.com an imaginary hot tub ...

Vito 'ZeD' De Tullio

unread,
Mar 16, 2010, 3:17:43 PM3/16/10
to pytho...@python.org
Grant Edwards wrote:

>> As for not being able to see the difference between a hyphen and an
>> EN- dash, or minus sign, or whatever it is, yes but they are very
>> similar looking glyphs in virtually ever modern font. It would take a
>> good eye to see the difference between (say) ??? ??? and -.
>
> My point is that if it's an ASCII file,

source files aren't (necessary) ASCII files

--
By ZeD

Grant Edwards

unread,
Mar 16, 2010, 4:12:29 PM3/16/10
to

OK, if it's a file with encoding <whatever>, then characters that
aren't part of <whatever> shouldn't be rendered according so some
other arbitrary character set. Illegal characters should be flagged
and displayed in a manner that makes it obvious you have illegal
characters in in the file.

If the OP was using a source file with an encoding that isn't accepted
by Python, then he needs to pick a source file encoding that is
recognized by Python.

Using a source file encoding where there are semantically _different_
characters that render identically is just asking for pain...

--
Grant Edwards grant.b.edwards Yow! I guess you guys got
at BIG MUSCLES from doing too
gmail.com much STUDYING!

Albert van der Horst

unread,
Mar 22, 2010, 10:26:10 AM3/22/10
to
In article <56597268-3472-4fd9...@e7g2000yqf.googlegroups.com>,

Make a hex-dump of your file. How does line 70 look?
If you see
0900 66 66 96 48 0A -- ff.H.

you know you are doing something illegal.

>
>Thanks in advance

Groetjes Albert

P.S. With all due respect, error messages come not any clearer than
that!

--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- being exponential -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst

0 new messages