Google Appengine 像文件一样从 TextProperty 或 BlobProperty 中逐行读取文本
我正在开发一个 Google Appengine Python 项目,用户将上传一个“csv 类型”文件,我可以将其存储在 TextProperty
或 BlobProperty
中,因为它们将在下面1MB。
我不明白的是如何稍后从 blob 中读取文件,并逐行处理它。
import csv
class Upload(db.Model):
#file = db.TextProperty(verbose_name='Uploaded File', required=True)
file = db.BlobProperty(verbose_name='Uploaded File', required=True)
# ...
def ProcessCsv(self):
csvReader = csv.reader(self.file,delimiter=',')
for row in csvReader:
print(', '.join(row))
print 只是打印出“文件”中每行的每个字符
有什么想法吗?
I am working on a Google Appengine Python project where users will upload a "csv type" file, which i can store in either a TextProperty
or BlobProperty
as they will be way under 1 mb.
What I can't figure out is how to read the file later from the blob, and process it line by line.
import csv
class Upload(db.Model):
#file = db.TextProperty(verbose_name='Uploaded File', required=True)
file = db.BlobProperty(verbose_name='Uploaded File', required=True)
# ...
def ProcessCsv(self):
csvReader = csv.reader(self.file,delimiter=',')
for row in csvReader:
print(', '.join(row))
print just prints out each character in the "file" on each line
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为发生的事情是当 csv.reader 需要文件对象或列表时,您将字符串传递给它。
尝试: self.file.split('\n') 将字符串转换为字符串列表。
I think what's going on is you are handing a string to csv.reader when it wants a file object or a list.
try: self.file.split('\n') to turn the string into a list of strings.
您需要一个 BlobReader,它是一个类似文件的对象,用于从 blob 中读取数据。您可以通过在 BlobInfo 对象上调用
.open()
来获取它,如下所示:You want a BlobReader, which is a file-like object for reading from a blob. You can get one by calling
.open()
on your BlobInfo object, like this: