相关疑难解决方法(0)

Python3中的StringIO

我使用的是Python 3.2.1,我无法导入StringIO模块.我使用 io.StringIO和它的作品,但我不能使用它numpygenfromtxt是这样的:

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)

python io python-3.x

407
推荐指数
7
解决办法
48万
查看次数

Python gzip:有没有办法从字符串解压缩?

我已经阅读了这篇关于这个问题的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)

python gzip

29
推荐指数
3
解决办法
4万
查看次数

GitPython 的“git show”输出出现“文件不是 zip 文件”错误

重现问题的脚本

将此代码保存为 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)

python git plugins

4
推荐指数
1
解决办法
649
查看次数

标签 统计

python ×3

git ×1

gzip ×1

io ×1

plugins ×1

python-3.x ×1