您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 带二进制文件的StringIO?

5b51 2022/1/14 8:20:49 python 字数 1476 阅读 458 来源 www.jb51.cc/python

我似乎得到了不同的输出:from StringIO import * file = open('1.bmp', 'r') print file.read(), '\n' print StringIO(file.read()).getvalue() 为什么?是因为StringIO只支持文本字符串或其他东西吗?最佳答案当你调用file.read()时,它会将

概述

我似乎得到了不同的输出

from StringIO import *

file = open('1.bmp','r')

print file.read(),'\n'
print StringIO(file.read()).getvalue()

为什么?是因为StringIO只支持文本字符串或其他东西吗?

相反,尝试例如重新打开文件

from StringIO import *

file = open('1.bmp','r')
print file.read(),'\n'
file.close()

file2 = open('1.bmp','r')
print StringIO(file2.read()).getvalue()
file2.close()

您还可以使用with语句使代码更清晰:

from StringIO import *

with open('1.bmp','r') as file:
    print file.read(),'\n'

with open('1.bmp','r') as file2:
    print StringIO(file2.read()).getvalue()

顺便说一句,我建议以二进制模式打开二进制文件:open(‘1.bmp’,’rb’)

总结

以上是编程之家为你收集整理的python – 带二进制文件的StringIO?全部内容,希望文章能够帮你解决python – 带二进制文件的StringIO?所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶