纳速健身

标题: Django python中 import cStringIO 错误提示 ModuleNotFoundError: No module named 'cStringIO' [打印本页]

作者: awagink    时间: 2020-8-27 09:17
标题: Django python中 import cStringIO 错误提示 ModuleNotFoundError: No module named 'cStringIO'

Django python中 import cStringIO 错误提示 ModuleNotFoundError: No module named 'cStringIO'

2.x写法:

    import cStringIO

3.x写法:

from io import StringIO



From Python 3.0 changelog;
The StringIO and cStringIO modules are gone. Instead, import the io module and use io.StringIO or io.BytesIO for text and data respectively.
From the Python 3 email documentation it can be seen that io.StringIO should be used instead:

  1. from io import StringIO
  2. from email.generator import Generator
  3. fp = StringIO()
  4. g = Generator(fp, mangle_from_=True, maxheaderlen=60)
  5. g.flatten(msg)
  6. text = fp.getvalue()
复制代码

从Python 3.0开始,StringIO和cStringIO模块已经取消。通过import io模块代替,分别使用io.String或io.BytesIO处理文本和数据。从Python 3邮件流文档能看到相关实现StringIO的代码为:

  1. from io import StringIO
  2. from email.generator import Generator
  3. fp = StringIO()
  4. g = Generator(fp, mangle_from_=True, maxheaderlen=60)
  5. g.flatten(msg)
  6. text = fp.getvalue()
复制代码









欢迎光临 纳速健身 (https://nasue.com/) Powered by Discuz! X3.4