subtraction is giving me a syntax error

J

Joel Pendery

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
 
P

Philip Semanchuk

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?


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.
 
M

MRAB

Joel said:
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

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

Grant Edwards

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. ;)
 
B

Baptiste Carvello

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.

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

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
 
S

Steven D'Aprano

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.
 
J

John Machin

Joel Pendery a écrit :





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)
 
G

Grant Edwards

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.
 
S

Steven D'Aprano

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 -.
 
G

Grant Edwards

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.
 
G

Grant Edwards

source files aren't (necessary) ASCII files

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...
 
A

Albert van der Horst

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?

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,175
Messages
2,570,946
Members
47,498
Latest member
yelene6679

Latest Threads

Top