本文介绍了Python 3.4:cStringIO与StringIO的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我返回一个ImportError:没有名为'cStringIO'的模块。不幸的是cStringIO不再存在了,我需要使用StringIO作为替代品。我怎么能这样做?

I am returning an ImportError: No module named 'cStringIO'. Unfortunately cStringIO doesn't exist anymore and I need to use StringIO as a replacement. How can I do this?

import edgar
import ftplib
from io import StringIO

ftp = ftplib.FTP(edgar.FTP_ADDR)
ftp.login()
try:
   edgar.download_all(ftp, "/tmp")
except Exception as e:
   print(e)
finally:
   ftp.close()



OUTPUT



OUTPUT

Traceback (most recent call last):
File "/usr/local/lib/ana/lib/python3.4/site-         packages/edgar/downloader.py", line 5, in <module>
from cStringIO import StringIO
ImportError: No module named 'cStringIO'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/home/aranjohn/PycharmProjects/edgar/secEd.py", line 1, in     <module>
import edgar
File "/usr/local/lib/ana/lib/python3.4/site-  packages/edgar/__init__.py", line 1, in <module>
from .downloader import FTP_ADDR, file_list, download, download_all
File "/usr/local/lib/ana/lib/python3.4/site-  packages/edgar/downloader.py", line 7, in <module>
from StringIO import StringIO
ImportError: No module named 'StringIO'

Process finished with exit code 1


推荐答案

StringIO 在3.x中不再存在。对于文本使用 io.StringIO 或使用 io.BytesIO 作为字节。

StringIO no longer exists in 3.x. Use either io.StringIO for text or io.BytesIO for bytes.

这篇关于Python 3.4:cStringIO与StringIO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-14 12:12