1

を使用して取得したファイルを GAE で開こうとしていますurlfetch()

これが私がこれまでに持っているものです:

from google.appengine.api import urlfetch

result = urlfetch.fetch('http://example.com/test.txt')
data = result.content
## f = open(...) <- what goes in here?

これは奇妙に思えるかもしれませんが、BlobStore には非常によく似た関数があり、blobfile に書き込むことができますdata

f = files.blobstore.create(mime_type='txt', _blobinfo_uploaded_filename='test')
with files.open(f, 'a') as data:
    data.write(result.content)

data任意のファイル オブジェクトに書き込むにはどうすればよいですか?

編集:もっと明確にする必要があります。ファイルをurlfetchしてファイルオブジェクトで開こうとしてresult.contentます。したがって、.txt ではなく .doc である可能性があります

4

2 に答える 2

3

StringIO モジュールを使用して、文字列の内容を使用してファイル オブジェクトをエミュレートできます。

from google.appengine.api import urlfetch
from StringIO import StringIO

result = urlfetch.fetch('http://example.com/test.txt')
f = StringIO(result.content)

その後、f オブジェクトから read() するか、seek()、readline() などの他のファイル オブジェクト メソッドを使用できます。

于 2012-10-02T03:20:33.520 に答える
2

ファイルを開く必要はありません。data = result.content で txt データを受け取りました。

于 2012-10-02T00:40:30.057 に答える