我想使用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'显然,这是一个错误的接口。什么才是正确的呢?
https://stackoverflow.com/questions/38103602
复制相似问题