Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #111561 > unrolled thread

SyntaxError: Non-ASCII character

Started byldompeling@casema.nl
First post2016-07-17 02:19 -0700
Last post2016-07-17 09:27 -0700
Articles 11 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  SyntaxError: Non-ASCII character ldompeling@casema.nl - 2016-07-17 02:19 -0700
    Re: SyntaxError: Non-ASCII character Chris Angelico <rosuav@gmail.com> - 2016-07-17 19:26 +1000
      Re: SyntaxError: Non-ASCII character Rustom Mody <rustompmody@gmail.com> - 2016-07-17 02:57 -0700
        Re: SyntaxError: Non-ASCII character Steven D'Aprano <steve@pearwood.info> - 2016-07-17 21:01 +1000
    Re: SyntaxError: Non-ASCII character ldompeling@casema.nl - 2016-07-17 05:01 -0700
      Re: SyntaxError: Non-ASCII character Chris Angelico <rosuav@gmail.com> - 2016-07-17 22:11 +1000
      Re: SyntaxError: Non-ASCII character Steven D'Aprano <steve@pearwood.info> - 2016-07-18 00:49 +1000
      Re: SyntaxError: Non-ASCII character Wildman <best_lay@yahoo.com> - 2016-07-17 11:07 -0500
    Re: SyntaxError: Non-ASCII character ldompeling@casema.nl - 2016-07-17 06:35 -0700
      Re: SyntaxError: Non-ASCII character Steven D'Aprano <steve@pearwood.info> - 2016-07-18 00:50 +1000
    Re: SyntaxError: Non-ASCII character ldompeling@casema.nl - 2016-07-17 09:27 -0700

#111561 — SyntaxError: Non-ASCII character

Fromldompeling@casema.nl
Date2016-07-17 02:19 -0700
SubjectSyntaxError: Non-ASCII character
Message-ID<b804e2c1-bf9c-44fc-8c8e-6616489aa8ba@googlegroups.com>
I copy this script from the magpi but when I run this script I get this error: 
SyntaxError: Non-ASCII character '\xe2' in file sound.py on line 32, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Below is the eamplescript. What is wrong with this script.


# You need to import the pyaudio module
import pyaudio
# First, we will listen
# We need to set some parameters
# Buffer chunk size in bytes
CHUNK = 1024
# The audio format
FORMAT = pyaudio.paInt16
# The number of channels to record on
CHANNELS = 2
# The sample rate, 44.1KHz
RATE = 44100
# The number of seconds to record for
RECORD_SECS = 5
# Next, we create a PyAudio object
p = pyaudio.PyAudio()
# We need a stream to record from
stream = p.open(format=FORMAT, channels=CHANNELS,
    rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
# We can now record into a temporary buffer
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECS)):
    data = stream.read(CHUNK)
    frames.append(data)
# We can now shut everything down
stream.stop_stream()
stream.close()
p.terminate()
# If we want to play a wave file, we will need the wave module
import wave
# We can open it, give a filename
wf = wave.open(“test.wav”, “rb”)
# We need a new PyAudio object
p = pyaudio.PyAudio()
# We will open a stream, using the settings from the wave file
stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
    channels=wf.getnchannels(), rate=wf.getframerate(),
    output=True)
# We can now read from the file and play it out
data = wf.readframes(CHUNK)
while data != ‘’:
    stream.write(data)
    data = wf.readframes(CHUNK)
# Don’t forget to shut everything down again
stream.stop_stream()
stream.close()
p.terminate()

[toc] | [next] | [standalone]


#111562

FromChris Angelico <rosuav@gmail.com>
Date2016-07-17 19:26 +1000
Message-ID<mailman.62.1468747601.2307.python-list@python.org>
In reply to#111561
On Sun, Jul 17, 2016 at 7:19 PM,  <ldompeling@casema.nl> wrote:
> wf = wave.open(“test.wav”, “rb”)

Watch your quotes. They want to be flat quotes, U+0022 "this sort",
not any sort of typographical quote.

Recommendation: Use a programmer's editor, not a word processor, for
working with code. As well as not mangling it, it'll often show you
problems before they happen.

Also, recommendation: Use Python 3, not Python 2. Your code looks like
it's intended for Py3, but your error says that you ran it under Py2.
The solution may be as simple as running "python3 script.py" rather
than "python script.py".

ChrisA

[toc] | [prev] | [next] | [standalone]


#111563

FromRustom Mody <rustompmody@gmail.com>
Date2016-07-17 02:57 -0700
Message-ID<5165e8ec-2be6-49ba-8654-2ca39d643d3f@googlegroups.com>
In reply to#111562
On Sunday, July 17, 2016 at 2:56:53 PM UTC+5:30, Chris Angelico wrote:
> On Sun, Jul 17, 2016 at 7:19 PM,   wrote:
> > wf = wave.open(“test.wav”, “rb”)
> 
> Watch your quotes. They want to be flat quotes, U+0022 "this sort",
> not any sort of typographical quote.
> 
> Recommendation: Use a programmer's editor, not a word processor, for
> working with code. As well as not mangling it, it'll often show you
> problems before they happen.
> 
> Also, recommendation: Use Python 3, not Python 2. Your code looks like
> it's intended for Py3, but your error says that you ran it under Py2.
> The solution may be as simple as running "python3 script.py" rather
> than "python script.py".

Happens when you cut paste… from web but especially from pdfs

I dare say that Python3’s :
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ariston/foo.py", line 31
    wf = wave.open(“test.wav”, “rb”)
                       ^
SyntaxError: invalid character in identifier

is probably worse than Python2’s

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "foo.py", line 31
SyntaxError: Non-ASCII character '\xe2' in file foo.py on line 31, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

[toc] | [prev] | [next] | [standalone]


#111568

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-17 21:01 +1000
Message-ID<578b657f$0$1590$c3e8da3$5496439d@news.astraweb.com>
In reply to#111563
On Sun, 17 Jul 2016 07:57 pm, Rustom Mody wrote:

> I dare say that Python3’s :
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/home/ariston/foo.py", line 31
>     wf = wave.open(“test.wav”, “rb”)
>                        ^
> SyntaxError: invalid character in identifier

If the caret ^ would line up with the invalid character, that would give a
hint to the reader which character was invalid. Or perhaps showing, or
naming, the invalid character in the error message:

SyntaxError: invalid character '“' in identifier


> is probably worse than Python2’s
> 
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "foo.py", line 31
> SyntaxError: Non-ASCII character '\xe2' in file foo.py on line 31, but no
> encoding declared; see http://python.org/dev/peps/pep-0263/ for details

I'm not sure. I think the Python 2 version is less useful: it doesn't show
you the line, you have to go look for it yourself. Then you have to work
out what character is \xe2, which is generally not easy because your editor
is unlikely to show the escape code but rather some apparently normal
looking character.



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

[toc] | [prev] | [next] | [standalone]


#111573

Fromldompeling@casema.nl
Date2016-07-17 05:01 -0700
Message-ID<b2859677-18d5-4819-bb9d-f87517c160f7@googlegroups.com>
In reply to#111561
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
> I copy this script from the magpi but when I run this script I get this error: 
> SyntaxError: Non-ASCII character '\xe2' in file sound.py on line 32, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
> 
> Below is the eamplescript. What is wrong with this script.
> 
> 
> # You need to import the pyaudio module
> import pyaudio
> # First, we will listen
> # We need to set some parameters
> # Buffer chunk size in bytes
> CHUNK = 1024
> # The audio format
> FORMAT = pyaudio.paInt16
> # The number of channels to record on
> CHANNELS = 2
> # The sample rate, 44.1KHz
> RATE = 44100
> # The number of seconds to record for
> RECORD_SECS = 5
> # Next, we create a PyAudio object
> p = pyaudio.PyAudio()
> # We need a stream to record from
> stream = p.open(format=FORMAT, channels=CHANNELS,
>     rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
> # We can now record into a temporary buffer
> frames = []
> for i in range(0, int(RATE / CHUNK * RECORD_SECS)):
>     data = stream.read(CHUNK)
>     frames.append(data)
> # We can now shut everything down
> stream.stop_stream()
> stream.close()
> p.terminate()
> # If we want to play a wave file, we will need the wave module
> import wave
> # We can open it, give a filename
> wf = wave.open(“test.wav”, “rb”)
> # We need a new PyAudio object
> p = pyaudio.PyAudio()
> # We will open a stream, using the settings from the wave file
> stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
>     channels=wf.getnchannels(), rate=wf.getframerate(),
>     output=True)
> # We can now read from the file and play it out
> data = wf.readframes(CHUNK)
> while data != ‘’:
>     stream.write(data)
>     data = wf.readframes(CHUNK)
> # Don’t forget to shut everything down again
> stream.stop_stream()
> stream.close()
> p.terminate()

I installed python 3.4 and set my python path to PYTONPATH:/usr/bin/python3.4

When I try to import pyaudio then I get this error:
Python 3.4.2 (default, Oct 19 2014, 13:31:11)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pyaudio
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'pyaudio'

Do I have to set a path also for to find the modules

[toc] | [prev] | [next] | [standalone]


#111575

FromChris Angelico <rosuav@gmail.com>
Date2016-07-17 22:11 +1000
Message-ID<mailman.65.1468757468.2307.python-list@python.org>
In reply to#111573
On Sun, Jul 17, 2016 at 10:01 PM,  <ldompeling@casema.nl> wrote:
> I installed python 3.4 and set my python path to PYTONPATH:/usr/bin/python3.4
>
> When I try to import pyaudio then I get this error:
> Python 3.4.2 (default, Oct 19 2014, 13:31:11)
> [GCC 4.9.1] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import pyaudio
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named 'pyaudio'
>
> Do I have to set a path also for to find the modules

You shouldn't need to set your python path. But, what you may need to
do is install pyaudio under Python 3. Whatever you did for Py2, do
again for Py3 - for instance:
$ sudo python3 -m pip install pyaudio

ChrisA

[toc] | [prev] | [next] | [standalone]


#111577

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-18 00:49 +1000
Message-ID<578b9b01$0$1585$c3e8da3$5496439d@news.astraweb.com>
In reply to#111573
On Sun, 17 Jul 2016 10:01 pm, ldompeling@casema.nl wrote:

> I installed python 3.4 and set my python path to
> PYTONPATH:/usr/bin/python3.4
> 
> When I try to import pyaudio then I get this error:
> Python 3.4.2 (default, Oct 19 2014, 13:31:11)
> [GCC 4.9.1] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import pyaudio
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named 'pyaudio'
> 
> Do I have to set a path also for to find the modules

No, but you do have to install pyaudio separately for each version of Python
you have.

So if you have installed Python 2.7 and 3.4, and Python 2.7 has pyaudio
installed, Python 3.4 cannot use the same one.



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

[toc] | [prev] | [next] | [standalone]


#111583

FromWildman <best_lay@yahoo.com>
Date2016-07-17 11:07 -0500
Message-ID<V_-dnRyZ4LHNMBbKnZ2dnUU7-cednZ2d@giganews.com>
In reply to#111573
On Sun, 17 Jul 2016 05:01:21 -0700, ldompeling wrote:

> I installed python 3.4 and set my python path to PYTONPATH:/usr/bin/python3.4
> 
> When I try to import pyaudio then I get this error:
> Python 3.4.2 (default, Oct 19 2014, 13:31:11)
> [GCC 4.9.1] on linux
> Type "help", "copyright", "credits" or "license" for more information.
>>>> import pyaudio
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
> ImportError: No module named 'pyaudio'

You need to install the Pytyon3 version of pyaudio...

sudo apt-get install python3-pyaudio

> Do I have to set a path also for to find the modules

No.

-- 
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!

[toc] | [prev] | [next] | [standalone]


#111576

Fromldompeling@casema.nl
Date2016-07-17 06:35 -0700
Message-ID<05605cfc-bc97-4dfe-bd6d-6a494079a684@googlegroups.com>
In reply to#111561
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
> I copy this script from the magpi but when I run this script I get this error: 
> SyntaxError: Non-ASCII character '\xe2' in file sound.py on line 32, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
> 
> Below is the eamplescript. What is wrong with this script.
> 
> 
> # You need to import the pyaudio module
> import pyaudio
> # First, we will listen
> # We need to set some parameters
> # Buffer chunk size in bytes
> CHUNK = 1024
> # The audio format
> FORMAT = pyaudio.paInt16
> # The number of channels to record on
> CHANNELS = 2
> # The sample rate, 44.1KHz
> RATE = 44100
> # The number of seconds to record for
> RECORD_SECS = 5
> # Next, we create a PyAudio object
> p = pyaudio.PyAudio()
> # We need a stream to record from
> stream = p.open(format=FORMAT, channels=CHANNELS,
>     rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
> # We can now record into a temporary buffer
> frames = []
> for i in range(0, int(RATE / CHUNK * RECORD_SECS)):
>     data = stream.read(CHUNK)
>     frames.append(data)
> # We can now shut everything down
> stream.stop_stream()
> stream.close()
> p.terminate()
> # If we want to play a wave file, we will need the wave module
> import wave
> # We can open it, give a filename
> wf = wave.open(“test.wav”, “rb”)
> # We need a new PyAudio object
> p = pyaudio.PyAudio()
> # We will open a stream, using the settings from the wave file
> stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
>     channels=wf.getnchannels(), rate=wf.getframerate(),
>     output=True)
> # We can now read from the file and play it out
> data = wf.readframes(CHUNK)
> while data != ‘’:
>     stream.write(data)
>     data = wf.readframes(CHUNK)
> # Don’t forget to shut everything down again
> stream.stop_stream()
> stream.close()
> p.terminate()

I hate it to ask you again but I really want that this script going to work.
And I use the raspberry pi for it.

I also get a lot off alsa errors on my screen so I don't no if that result in this error:

Traceback (most recent call last):
  File "sound.py", line 19, in <module>
    rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
NameError: name 'TRUE' is not defined

[toc] | [prev] | [next] | [standalone]


#111578

FromSteven D'Aprano <steve@pearwood.info>
Date2016-07-18 00:50 +1000
Message-ID<578b9b2c$0$1591$c3e8da3$5496439d@news.astraweb.com>
In reply to#111576
On Sun, 17 Jul 2016 11:35 pm, ldompeling@casema.nl wrote:

> I also get a lot off alsa errors on my screen so I don't no if that result
> in this error:
> 
> Traceback (most recent call last):
> File "sound.py", line 19, in <module>
> rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
> NameError: name 'TRUE' is not defined

Try using input=True instead.



-- 
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

[toc] | [prev] | [next] | [standalone]


#111584

Fromldompeling@casema.nl
Date2016-07-17 09:27 -0700
Message-ID<5d689fa9-323e-4da2-8e00-48e2f8a5c88b@googlegroups.com>
In reply to#111561
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
> I copy this script from the magpi but when I run this script I get this error: 
> SyntaxError: Non-ASCII character '\xe2' in file sound.py on line 32, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
> 
> Below is the eamplescript. What is wrong with this script.
> 
> 
> # You need to import the pyaudio module
> import pyaudio
> # First, we will listen
> # We need to set some parameters
> # Buffer chunk size in bytes
> CHUNK = 1024
> # The audio format
> FORMAT = pyaudio.paInt16
> # The number of channels to record on
> CHANNELS = 2
> # The sample rate, 44.1KHz
> RATE = 44100
> # The number of seconds to record for
> RECORD_SECS = 5
> # Next, we create a PyAudio object
> p = pyaudio.PyAudio()
> # We need a stream to record from
> stream = p.open(format=FORMAT, channels=CHANNELS,
>     rate=RATE, input=TRUE, frames_per_buffer=CHUNK)
> # We can now record into a temporary buffer
> frames = []
> for i in range(0, int(RATE / CHUNK * RECORD_SECS)):
>     data = stream.read(CHUNK)
>     frames.append(data)
> # We can now shut everything down
> stream.stop_stream()
> stream.close()
> p.terminate()
> # If we want to play a wave file, we will need the wave module
> import wave
> # We can open it, give a filename
> wf = wave.open(“test.wav”, “rb”)
> # We need a new PyAudio object
> p = pyaudio.PyAudio()
> # We will open a stream, using the settings from the wave file
> stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
>     channels=wf.getnchannels(), rate=wf.getframerate(),
>     output=True)
> # We can now read from the file and play it out
> data = wf.readframes(CHUNK)
> while data != ‘’:
>     stream.write(data)
>     data = wf.readframes(CHUNK)
> # Don’t forget to shut everything down again
> stream.stop_stream()
> stream.close()
> p.terminate()

When I change input=True instead than I get a lot of errors:
Traceback (most recent call last):
  File "sound.py", line 19, in <module>
    rate=RATE, input=True, frames_per_buffer=CHUNK)
  File "/usr/lib/python3/dist-packages/pyaudio.py", line 747, in open
    stream = Stream(self, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/pyaudio.py", line 442, in __init__
    self._stream = pa.open(**arguments)
OSError: [Errno Invalid number of channels] -9998

I don't think we can solve those errors
Anyway thanks for helping me out so far.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web