regular expression, unicode

S

Simon Strobl

Hello,

why can't I use this pattern

good = re.compile("^[A-ZÄÖÜ].*")

in python3. According to the documentation, patterns may be unicode
strings.

I get this error message:

Traceback (most recent call last):
File "./get.py", line 8, in <module>
for line in sys.stdin:
File "/usr/lib64/python3.0/io.py", line 1734, in __next__
line = self.readline()
File "/usr/lib64/python3.0/io.py", line 1808, in readline
while self._read_chunk():
File "/usr/lib64/python3.0/io.py", line 1557, in _read_chunk
self._set_decoded_chars(self._decoder.decode(input_chunk, eof))
File "/usr/lib64/python3.0/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1:
invalid data

Simon
 
M

MRAB

Simon said:
Hello,

why can't I use this pattern

good = re.compile("^[A-ZÄÖÜ].*")

in python3. According to the documentation, patterns may be unicode
strings.

I get this error message:

Traceback (most recent call last):
File "./get.py", line 8, in <module>
for line in sys.stdin:
File "/usr/lib64/python3.0/io.py", line 1734, in __next__
line = self.readline()
File "/usr/lib64/python3.0/io.py", line 1808, in readline
while self._read_chunk():
File "/usr/lib64/python3.0/io.py", line 1557, in _read_chunk
self._set_decoded_chars(self._decoder.decode(input_chunk, eof))
File "/usr/lib64/python3.0/codecs.py", line 300, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1:
invalid data
In Python 3 .py files are assumed to be encoded in UTF-8 unless declared
otherwise by a line such as:

# -*- coding: cp-1252 -*-

You need to check what encoding your editor is using (if possible use
UTF-8).
 

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
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top