1
Я бы хотел использовать pygments, чтобы выделить код для меня на лету - в основном огромный список объектов JSON. Вот что я пробовал:Поток строки в лекции легарей?
from pygments.lexers import JsonLexer
from pygments.formatters import HtmlFormatter
from pygments import highlight
import StringIO
f = StringIO.StringIO()
f.write('a')
f.seek(0)
print highlight(f, JsonLexer(), HtmlFormatter())
Это дало мне следующую ошибку:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/d33tah/virtualenv/lib/python2.7/site-packages/pygments/__init__.py", line 87, in highlight
return format(lex(code, lexer), formatter, outfile)
File "/home/d33tah/virtualenv/lib/python2.7/site-packages/pygments/__init__.py", line 45, in lex
return lexer.get_tokens(code)
File "/home/d33tah/virtualenv/lib/python2.7/site-packages/pygments/lexer.py", line 151, in get_tokens
text, _ = guess_decode(text)
File "/home/d33tah/virtualenv/lib/python2.7/site-packages/pygments/util.py", line 309, in guess_decode
text = text.decode('utf-8')
AttributeError: StringIO instance has no attribute 'decode'
Очевидно, что это является неправильным интерфейсом. Что было бы правильным?