
1 likexian Feb 26, 2014 fout = open("输出文件路径","wb") img = x.photo.file.read() fout.write(img) |
4 est Feb 26, 2014 fout.close() |
5 lucifer9 Feb 26, 2014 tempfile.SpooledTemporaryFile ? |
6 tonic Feb 26, 2014 from cStringIO import StringIO? |
7 lynx Feb 26, 2014 python2: str, (c)StringIO.StringIO python3: bytes, io.BytesIO 这些都可以 |
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 类型的字符串就会出错。 |