Найти - Пользователи
Полная версия: словари и utf8
Начало » Python для новичков » словари и utf8
1 2
vladimirse
Вот этот код:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import codecs
with codecs.open('input1.txt', 'r', 'utf-8') as file:
	for l in file:
		print l

Вот такую ошибку выдает:
Traceback (most recent call last):
  File "rus_print_infile.py", line 7, in <module>
    for l in file:
  File "/usr/lib/python2.7/codecs.py", line 684, in next
    return self.reader.next()
  File "/usr/lib/python2.7/codecs.py", line 615, in next
    line = self.readline()
  File "/usr/lib/python2.7/codecs.py", line 530, in readline
    data = self.read(readsize, firstline=True)
  File "/usr/lib/python2.7/codecs.py", line 477, in read
    newchars, decodedbytes = self.decode(data, self.errors)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 12: invalid continuation byte


Работаю в Линуксе, локаль utf-8.
sergeek
там видимо все-таки не utf-8
vladimirse
Ну если локаль utf-8 и текстовый файл я создаю:
cat > input.txt
бла...бла...бла...

то как там может быть не utf-8?
malya
Прикрепите Ваш текстовый файл.
Проверьте:
$ file input.txt 
input.txt: UTF-8 Unicode text
vladimirse
Да я его так и проверял )))
$ file input.txt 
input.txt: UTF-8 Unicode text

malya
Ну если можно то и сам *.py файл.

З.Ы.
С вашим файлом у меня работают два варианта которые я постил.
vladimirse
В общем ошибка была в том, что я я писал:
inputfile.readline()
вместо
inputfile.readlines()

Всем спасибо за помощь!

This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB