用 Python 如何在内存中建立一个二进制数组对象? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
sedgwickz

用 Python 如何在内存中建立一个二进制数组对象?

  •  
  •   sedgwickz Feb 26, 2014 4172 views
    This topic created in 4442 days ago, the information mentioned may be changed or developed.
    描述可能不够准确,大致是这样的,我使用web.py框架,使用上传组件在后台可以得到这个文件的unicode流对象。
    img = x.photo.file.read()

    我如何根据这个x.photo.file得到它的二进制呢?

    网上之前有人是这样做的。
    fout = open("输出文件路径","rb")
    img = x.photo.file.read()
    fout.write(img)

    但我这里并没有输出文件,他只是临时在内存中存在,当以byte[]形式提交给服务器后就自动释放了。
    求指点,谢谢。

    我在stackoverflow也发问了,还木有人回复。
    http://stackoverflow.com/questions/22032639/how-to-get-file-binary-fomat-in-behind-code-of-web-py-framework
    Supplement 1    Feb 27, 2014
    感谢各位的回复。查询了一些资料后已经解决了。

    附上我的Gist链接。希望能帮助到有类似需求的朋友。

    8 replies    1970-01-01 08:00:00 +08:00
    likexian
        1
    likexian  
       Feb 26, 2014   1
    fout = open("输出文件路径","wb")
    img = x.photo.file.read()
    fout.write(img)
    sedgwickz
        2
    sedgwickz  
    OP
       Feb 26, 2014
    sedgwickz
        3
    sedgwickz  
    OP
       Feb 26, 2014
    @likexian 不好意思 是我的错 确实是你这么写的 但是我本地没有要输出的文件路径,如何在内存中建立一个对象呢?
    est
        4
    est  
       Feb 26, 2014
    fout.close()
    lucifer9
        5
    lucifer9  
       Feb 26, 2014
    tempfile.SpooledTemporaryFile ?
    tonic
        6
    tonic  
       Feb 26, 2014
    from cStringIO import StringIO?
    lynx
        7
    lynx  
       Feb 26, 2014
    python2: str, (c)StringIO.StringIO
    python3: bytes, io.BytesIO

    这些都可以
    muzuiget
        8
    muzuiget  
       Feb 26, 2014
    你没说清楚你的 Python 版本,假设你是 Python 2,Python 2 的字符串有两种类型

    1. str 类型,这是默认的,就是 a = '中文',得到 '\xe4\xb8\xad\xe6\x96\x87'
    2. unicode 类型,a = u'中文',得到 u'\u4e2d\u6587'

    你所谓的「二进制数组」对象就是 str 类型的字符串。所以你的代码 img = x.photo.file.read(),img 不是你认为的「unicode流对象」,本来就是你想要的。

    fout.write 接受的参数就是需要一个 str 类型的字符串,如果是 unicode 类型的字符串就会出错。
    About     Help     Advertise     Blog     API     FAQ     Solana     1033 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 41ms UTC 23:07 PVG 07:07 LAX 16:07 JFK 19:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86