我使用的是Python 3.2.1,我无法导入StringIO模块.我使用
io.StringIO和它的作品,但我不能使用它numpy的genfromtxt是这样的:
x="1 3\n 4.5 8"
numpy.genfromtxt(io.StringIO(x))
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
TypeError: Can't convert 'bytes' object to str implicitly
Run Code Online (Sandbox Code Playgroud)
当我写import StringIO它时说
ImportError: No module named 'StringIO'
Run Code Online (Sandbox Code Playgroud) 我已经阅读了这篇关于这个问题的SO帖子无济于事.
我正在尝试解压缩来自URL的.gz文件.
url_file_handle=StringIO( gz_data )
gzip_file_handle=gzip.open(url_file_handle,"r")
decompressed_data = gzip_file_handle.read()
gzip_file_handle.close()
Run Code Online (Sandbox Code Playgroud)
...但我得到TypeError:强制转换为Unicode:需要字符串或缓冲区,找到cStringIO.StringI
这是怎么回事?
Traceback (most recent call last):
File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2974, in _HandleRequest
base_env_dict=env_dict)
File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 411, in Dispatch
base_env_dict=base_env_dict)
File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2243, in Dispatch
self._module_dict)
File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2161, in ExecuteCGI
reset_modules = exec_script(handler_path, cgi_path, hook)
File "/opt/google/google_appengine-1.2.5/google/appengine/tools/dev_appserver.py", line 2057, in ExecuteOrImportScript
exec module_code in script_module.__dict__
File "/home/jldupont/workspace/jldupont/trunk/site/app/server/tasks/debian/repo_fetcher.py", line 36, in <module>
main()
File "/home/jldupont/workspace/jldupont/trunk/site/app/server/tasks/debian/repo_fetcher.py", line 30, in main
gziph=gzip.open(fh,'r')
File "/usr/lib/python2.5/gzip.py", line 49, …Run Code Online (Sandbox Code Playgroud) 将此代码保存为 shell 脚本并运行它。该代码应该报告File is not a zip file错误。
#!/bin/bash
set -eu
mkdir foo
cd foo
pip install --user GitPython
echo foo > a
zip a.zip a
# -t option validates the zip file.
# See https://unix.stackexchange.com/questions/197127/test-integrity-of-zip-file
unzip -t a.zip
git init
git add a.zip
git commit -m 'init commit'
cat << EOF > test.py
from git import Repo
import zipfile
from io import StringIO
repo = Repo('.', search_parent_directories=True)
raw = repo.git.show("HEAD:a.zip")
z = zipfile.ZipFile(StringIO(raw), "r")
EOF
python3 …Run Code Online (Sandbox Code Playgroud)