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

SyntaxError: Non-ASCII character

178 views
Skip to first unread message

ldomp...@casema.nl

unread,
Jul 17, 2016, 5:19:32 AM7/17/16
to
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()

Chris Angelico

unread,
Jul 17, 2016, 5:26:53 AM7/17/16
to
On Sun, Jul 17, 2016 at 7:19 PM, <ldomp...@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

Rustom Mody

unread,
Jul 17, 2016, 5:57:44 AM7/17/16
to
On Sunday, July 17, 2016 at 2:56:53 PM UTC+5:30, Chris Angelico wrote:
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

Steven D'Aprano

unread,
Jul 17, 2016, 7:01:29 AM7/17/16
to
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.

ldomp...@casema.nl

unread,
Jul 17, 2016, 8:01:33 AM7/17/16
to
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
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

Chris Angelico

unread,
Jul 17, 2016, 8:11:21 AM7/17/16
to
On Sun, Jul 17, 2016 at 10:01 PM, <ldomp...@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

ldomp...@casema.nl

unread,
Jul 17, 2016, 9:37:50 AM7/17/16
to
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
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

Steven D'Aprano

unread,
Jul 17, 2016, 10:49:49 AM7/17/16
to
On Sun, 17 Jul 2016 10:01 pm, ldomp...@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 D'Aprano

unread,
Jul 17, 2016, 10:50:30 AM7/17/16
to
On Sun, 17 Jul 2016 11:35 pm, ldomp...@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.

Wildman

unread,
Jul 17, 2016, 12:07:59 PM7/17/16
to
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!

ldomp...@casema.nl

unread,
Jul 17, 2016, 12:27:35 PM7/17/16
to
Op zondag 17 juli 2016 11:19:32 UTC+2 schreef ldomp...@casema.nl:
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.

0 new messages