首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将字符串流到pygments lexer?

将字符串流到pygments lexer?
EN

Stack Overflow用户
提问于 2016-06-29 23:11:35
回答 0查看 265关注 0票数 1

我想使用pygments来动态地为我突出显示代码--基本上是一个巨大的JSON对象列表。这是我尝试过的:

代码语言:javascript
复制
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())

这给了我以下错误:

代码语言:javascript
复制
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'

显然,这是一个错误的接口。什么才是正确的呢?

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38103602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档