Appengine 创建和导出 yaml 文件

发布于 2024-09-27 21:06:13 字数 2913 浏览 6 评论 0原文

我在创建文件并导出到 .yaml 时遇到问题。 我正在使用 Google App Engine 和 Python 2.5。 不明白 Yaml 文档,这让我很困惑。

我想要的是创建一个文件并保存它。有必要从模型中获取实体。

class SaveYAML(webapp.RequestHandler):
    def post(self):
        user = db.Query(models.User)
        user = user.filter('user =', users.get_current_user())
        users = user.fetch(limit = 1)

        for user in users:
            print(user.name, user.adress, user.phone, user.city)

        self.response.headers['Content-Type'] = 'application/yaml'
        self.response.headers['Content-Disposition'] = 'filename = myYaml.yaml' 

通过此代码片段,当我单击按钮时,我可以在浏览器中查看从模型检索的信息。 也许是因为 print 方法,但它不会创建文件 但是当我将我的应用程序上传到 Google App Engine 时,它​​没有显示相同的信息。它仅显示“状态 200 正常”。

有人能指出我正确的方向吗? 我必须导入 pyyaml 库吗?

我将一些代码更改为:

print(yaml.dump(user, sys.stdout))

结果在浏览器中是这样的:

- !!binary |
  /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
  HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
  MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAnACgDASIA
  AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
  AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
  ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
  p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb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Z/kEgjY/MpXoe5HP/wCv+ZRRTEf/2Q==
_name: !!python/unicode 'Ana Ferreira'
_parent: null
_parent_key: null
_adress: !!python/unicode 'Porto'
_phone: !!python/unicode '1234569789'
_user: *id002
None
Status: 200 OK
Cache-Control: no-cache
Content-Type: text/yaml
Content-Disposition: filename = myYaml.yaml
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 0

更改了代码,但数据仅由浏览器呈现。

I´m having trouble in creating a file and export to .yaml.
I´m using Google App Engine with Python 2.5.
Don´t understand the Yaml doc´s, it makes me confused.

What i want is to create a file and save it. It´s necessary to get entities from Models.

class SaveYAML(webapp.RequestHandler):
    def post(self):
        user = db.Query(models.User)
        user = user.filter('user =', users.get_current_user())
        users = user.fetch(limit = 1)

        for user in users:
            print(user.name, user.adress, user.phone, user.city)

        self.response.headers['Content-Type'] = 'application/yaml'
        self.response.headers['Content-Disposition'] = 'filename = myYaml.yaml' 

With this snippet i can view in a browser, when i click in a button the information retrieved from models.
Maybe it´s because of print method, but it doesn´t create a file
But when i upload my app to Google App Engine it doesn´t show the same info. It shows only 'Status 200 Ok'.

Can someone point me in the right direction?
Do i have to import pyyaml library?

I changed some code to:

print(yaml.dump(user, sys.stdout))

and the result was this in a browser:

- !!binary |
  /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0a
  HBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIy
  MjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAnACgDASIA
  AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
  AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY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MAcfvJcyv9W5orWhKGFDEQU2jaR0xR
  XRCKhFRjsjOUnJuT6mALmOIyxujHMjE4478d6kXUUXIHnAZ/vH8e9FFUIY2roq8+dn03HH86wYby
  3bxFLduji3twYolxk5IyT196KKTipWv0GpNXsbNv4gtYZ/kEgjY/MpXoe5HP/wCv+ZRRTEf/2Q==
_name: !!python/unicode 'Ana Ferreira'
_parent: null
_parent_key: null
_adress: !!python/unicode 'Porto'
_phone: !!python/unicode '1234569789'
_user: *id002
None
Status: 200 OK
Cache-Control: no-cache
Content-Type: text/yaml
Content-Disposition: filename = myYaml.yaml
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Content-Length: 0

Changed the code, but the data is only presented by browser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

反差帅 2024-10-04 21:06:13

不要使用 print,使用 self.response.out.write(...)。

是的,您将需要导入 yaml 来输出 yaml,这会让事情变得更容易。

试试这个:

import yaml

users = model.Users.all().fetch(10)
users = [{'user': {'name': user.name,
                   'address': user.address,
                   'phone': user.phone,
                   'city': user.city}}
          for user in users]

self.response.out.write(yaml.dump(users, default_flow_style=False))

您可以查看 yaml 文档 以获取有关格式化输出的更多信息。

Do not use print, use self.response.out.write(...).

Yes, you will want to import yaml to output yaml, it will make it easier.

Try this:

import yaml

users = model.Users.all().fetch(10)
users = [{'user': {'name': user.name,
                   'address': user.address,
                   'phone': user.phone,
                   'city': user.city}}
          for user in users]

self.response.out.write(yaml.dump(users, default_flow_style=False))

You can check out the yaml docs for additional information on formatting the output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文