我错过了什么,如何让这个功能起作用?
import dateutil.parser
import datetime
my_date = datetime.datetime(2000, 1, 1, 0, 0, 0, 000000, tzinfo=tzutc())
print(my_date)
Run Code Online (Sandbox Code Playgroud)
给我错误:
NameError: name 'tzutc' is not defined
Run Code Online (Sandbox Code Playgroud) 我需要一种有效的方法来编写包含字典(包括日期时间)的文件,然后能够将它们作为字典读取。像这样的字典:
my_dict = {'1.0': [datetime.datetime(2000, 1, 1, 0, 0, 0, 000000, tzinfo=tzutc())], '2.0': [datetime.datetime(2000, 1, 1, 0, 0, 0, 000000, tzinfo=tzutc())]}
Run Code Online (Sandbox Code Playgroud)
尝试使用 json 转储:
with open("my_file.json", 'w+') as f:
json.dump(my_dict, f)
Run Code Online (Sandbox Code Playgroud)
TypeError: Object of type 'datetime' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
还尝试将整个 dict 写为一个字符串,然后用 yaml 导入它,这几乎有效,但索引混乱。
with open("my_file", 'w+') as f:
f.write(str(my_dict))
with open("my_file", 'r') as f:
s = f.read()
new_dict = yaml.load(s)
print(new_dict['1.0'][0])
Run Code Online (Sandbox Code Playgroud)
Output: datetime.datetime(2000
Expected: 2000-01-01 00:00:00+00:00
Run Code Online (Sandbox Code Playgroud) 我正在使用 python 2.7 和 boto3。我想不出一种在 python 中向 SES 添加附件的方法。我找到的最接近的是这个页面。
到目前为止,我所拥有的是:
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
import boto3
# via http://codeadict.wordpress.com/2010/02/11/send-e-mails-with-attachment-in-python/
ses = boto3.client('ses')
msg = MIMEMultipart()
msg['Subject'] = 'weekly report'
msg['From'] = email
msg['To'] = other_email
# what a recipient sees if they don't use an email reader
msg.preamble = 'Multipart message.\n'
# the message body
part = MIMEText('Howdy -- here is the data from last week.')
msg.attach(part)
# the attachment …Run Code Online (Sandbox Code Playgroud)