在Python中将str数据转换为文件对象 [英] Converting str data to file object in Python

查看:145
本文介绍了在Python中将str数据转换为文件对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将视频发布到Google Cloud Buckets,并且签名的PUT URL可以解决问题.但是,如果文件大小大于10MB,将无法使用,因此我找到了一个开放源代码,可以使用此文件,例如对象.

I am posting videos to Google Cloud Buckets and a signed PUT url does the trick. However, if the file size is greater than 10MB it will not work, so I found an open source that will allow me to do this however, it uses a file like object.

def read_in_chunks(file_object, chunk_size=65536):
while True:
    data = file_object.read(chunk_size)
    if not data:
        break
    yield data

def main(file, url):
content_name = str(file)
content_path = os.path.abspath(file)
content_size = os.stat(content_path).st_size

print content_name, content_path, content_size

f = open(content_path)

index = 0
offset = 0
headers = {}

for chunk in read_in_chunks(f):
    offset = index + len(chunk)
    headers['Content-Type'] = 'application/octet-stream'
    headers['Content-length'] = content_size
    headers['Content-Range'] = 'bytes %s-%s/%s' % (index, offset, content_size)
    index = offset
    try:
        r = requests.put(url, data=chunk, headers=headers)
        print "r: %s, Content-Range: %s" % (r, headers['Content-Range'])
    except Exception, e:
        print e

我上传视频的方式是传递json格式的数据.

The way that I was uploading videos was passing in json formatted data.

class GetData(webapp2.RequestHandler):
def post(self):
    data = self.request.get('file')

然后我要做的只是一个request.put(url,data = data).这无缝地工作.

Then all I did was a request.put(url, data=data). This worked seamlessly.

如何将Python识别为str的数据转换为对象之类的文件?

How do I convert this data, that Python recognizes as str to a file like object?

推荐答案

在大多数情况下,所谓的类似于文件"的对象只是实现Python缓冲区接口的对象.也就是说,具有 read write seek 等方法.

A so called 'file-like' object is in most cases just an object that implements the Python buffer interface; that is, has methods like read, write, seek, and so on.

用于缓冲区接口工具的标准库模块称为 io .您正在寻找 io.StringIO io.BytesIO ,具体取决于您拥有的数据类型-如果它是Unicode编码的字符串,则应该使用 io.StringIO ,但是您可能正在使用原始字节流(例如在图片文件),而不是纯文本,因此您要查找 io.BytesIO .处理文件时,这与对unicode文件进行 open(path,'r')和对原始文件进行 open(path,'rb')相同.字节.

The standard library module for buffer interface tools is called io. You're looking for either io.StringIO or io.BytesIO, depending on the type of data you have — if it's a unicode encoded string, you're supposed to use io.StringIO, but you're probably working with a raw bytestream (such as in an image file) as opposed to just text, so io.BytesIO is what you're looking for. When working with files, this is the same distinction as doing open(path, 'r') for unicode files and open(path, 'rb') for raw processing of the bytes.

两个类都将文件状对象的数据作为第一个参数,因此您只需执行以下操作:

Both classes take the data for the file-like object as the first parameter, so you just do:

f = io.BytesIO(b'test data')

此后, f 将是一个像文件一样工作的对象,除了它的数据保存在内存中而不是磁盘上.

After this, f will be an object that works just like a file, except for the fact that it holds its data in memory as opposed to on disk.

这篇关于在Python中将str数据转换为文件对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆