尝试在Python 2.7中将行写入文本文件,并具有以下代码:
# -*- coding: utf-8 -*-
...
f = open(os.path.join(os.path.dirname(__file__), 'output.txt'), 'w')
f.write('Smith’s BaseBall Cap') // Note the strangely shaped apostrophe
Run Code Online (Sandbox Code Playgroud)
但是,在output.txt中,我得到了Smith‚Äôs BaseBall Cap.不确定如何纠正这个编码问题?有这种问题的任何准备吗?
假设我有一个Person对象列表,它有age和room_number属性,我写了一个check()函数,如果person.age()和person.room_number()是满意的,则返回True,否则返回False.
filter(check, list_of_people_objects) 将返回满足条件的Person对象列表 check()
但是,我的问题是,是否有一种方法可以返回每个已批准人员的房间号码列表,而无需重复列表两次,如此不使用列表理解?所以过滤,但返回一个更具体的iterable属性.
map(lambda x: x.room_number(), filter(check, list_of_people_objects))
我有一个产品模型,与评级模型具有一对多关系。我之前在产品模型中存储了 Average_stars 字段,每次为该产品模型添加新的评级时我都会更新该字段。我将在views.py 中有一个函数,它将返回所有Product 实例的QuerySet,按average_star 排序。有没有办法使用 .aggregate 和 .order_by 的组合或类似的任何内容更动态地执行此操作。
换句话说,是否有一种方法可以根据每个产品各自的评级模型计算平均值,并按该属性对它们进行排序?哪种方法更好?
Pyramid 新手,并尝试设置 pyramid_mailer 向自己发送电子邮件:
我在 development.ini 中有:
mail.host = smtp.gmail.com
mail.username = EMAIL@gmail.com
mail.password = PASSWORD
mail.port = 587
mail.ssl = True
[handlers]
keys = console
Run Code Online (Sandbox Code Playgroud)
在我的项目/__init__.py 中:
config.include('pyramid_mailer')
Run Code Online (Sandbox Code Playgroud)
在我的项目/views.py
from pyramid_mailer.mailer import Mailer
from pyramid_mailer import get_mailer
from pyramid_mailer.message import Message
Run Code Online (Sandbox Code Playgroud)
Run Code Online (Sandbox Code Playgroud)@view_config(renderer="templates/site_view.pt") def site_view(self):
...
config.registry['mailer'] = Mailer.from_settings(settings)
mailer = request.registry['mailer']
message = Message(subject="It works!",
sender="EMAIL@gmail.cm",
recipients=["EMAIL@gmail.com"],
body="Hey there!")
mailer.send(message)
Run Code Online (Sandbox Code Playgroud)
我在这里错过了一些非常基本的东西吗?
编写了以下两个函数,用于存储和检索任何 Python(内置或用户定义)对象,结合 json 和 jsonpickle(2.7 中)
def save(kind, obj):
pickled = jsonpickle.encode(obj)
filename = DATA_DESTINATION[kind] \\returns file destination to store json
if os.path.isfile(filename):
open(filename, 'w').close()
with open(filename, 'w') as f:
json.dump(pickled, f)
def retrieve(kind):
filename = DATA_DESTINATION[kind] \\returns file destination to store json
if os.path.isfile(filename):
with open(filename, 'r') as f:
pickled = json.load(f)
unpickled = jsonpickle.decode(pickled)
print unpickled
Run Code Online (Sandbox Code Playgroud)
我还没有用用户定义的对象测试这两个函数,但是当我尝试 save() 一个内置的字符串字典时,(即 {'Adam': 'Age 19', 'Bill', 'Age 32 '}),然后我检索同一个文件,我得到同一个 unicode 的字典,{u'Adam': u'Age 19', u'Bill', u'Age 32'}。我认为 json/jsonpickle 默认编码为 utf-8,这是怎么回事?
[更新]:删除所有 jsonpickle …
有没有办法在具有多个属性的用户定义的Python对象列表上迭代和调用函数?我们假设它叫做Entry,带有属性名称和年龄.
这样我就能说些什么了
def func(name, age):
//do something
def start(list_of_entries)
map(func, list_of_entries.name(), list_of_entries.age())
//but obviously the .name and .age of the object, not the iterable
//these are the only two attributes of the class
Run Code Online (Sandbox Code Playgroud)
正在考虑使用functools.partial()但不确定在这种情况下它是否有效.