如何将多条记录添加到谷歌应用程序引擎数据存储中
我正在尝试通过上传格式化文件(包含多行)来更新数据存储。文件中的每一行都会在 Google App Engine 数据存储中创建一条新记录。我不确定如何在 for 循环中将多行添加到数据存储中。我尝试过:
class Records(db.Model):
something = db.StringProperty()
.....
for line in lines #lines is a file contains multiple lines
record = Records(parent = PARENT_KEY)
record.something = line
record.put()
它不起作用,错误消息是 BadValueError:属性某物不是多行。我猜测这是因为“记录”变量在每次迭代中引用相同的实例。
抱歉,如果这是一个愚蠢的问题。我对 python 和谷歌应用程序引擎很陌生。预先感谢您的意见!
I am trying to update a datastore by uploading a formatted file(contains multiple lines). Each line in the file will create a new record in google app engine datastore. I am not sure how to add multiple rows to the datastore within a for loop. I tried:
class Records(db.Model):
something = db.StringProperty()
.....
for line in lines #lines is a file contains multiple lines
record = Records(parent = PARENT_KEY)
record.something = line
record.put()
It doesn't work and the error message is BadValueError: Property something is not multi-line. I am guessing it's because the 'record' variable refer to the same instance across each iteration.
Sorry if it's a stupid question. I am pretty new to python and google app engine. Thanks in advance for your input!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
for line inlines
会将换行符保留在行尾; StringProperty 中不允许使用换行符,除非将其指定为多行。尝试
record.something = line.rstrip()
删除换行符。for line in lines
will keep the newline characters at the end of the lines; newlines aren't allowed in a StringProperty unless it's designated as multiline.try
record.something = line.rstrip()
to remove the newlines.您可以使用批量加载器将文件中的数据加载到数据存储中。
http://code.google.com/appengine/docs/python/tools /uploadingdata.html
You can use the bulk loader to load data from a file into the datastore.
http://code.google.com/appengine/docs/python/tools/uploadingdata.html