<p>只要在文件名中使用datetime,就可以在每次运行代码时创建一个唯一的文件。在这种情况下,粒度降低到每秒,因此,如果代码每秒运行一次以上,您将覆盖文件的现有内容。在这种情况下,请转到名称中包含微秒的文件名。你知道吗</p>
<pre><code>import datetime as dt
import json
time_script_run = dt.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')
with open('{}_data.json'.format(time_script_run), 'w') as outfile:
json.dump(Infotext, outfile)
</code></pre>
<p>这有多个缺点:</p>
<ol>
<li>你会有越来越多的文件</li>
<li>即使加载名称中包含最新日期时间的文件(并且发现该文件在运行时增长),也只能看到上次运行前的一次数据;很难查找完整的历史记录。你知道吗</li>
</ol>
<p>我认为最好使用轻量级数据库,例如<code>sqlite3</code>:</p>
<pre><code>import sqlite3
import random
import time
import datetime as dt
# Create DB
with sqlite3.connect('some_database.db') as conn:
c = conn.cursor()
# Just for this example, we'll clear the whole table to make it repeatable
try:
c.execute("DROP TABLE user_emails")
except sqlite3.OperationalError: # First time you run this code
pass
c.execute("""CREATE TABLE IF NOT EXISTS user_emails(
datetime TEXT,
first_name TEXT,
last_name TEXT,
email TEXT)
""")
# Now let's create some fake user behaviour
for x in range(5):
now = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
c.execute("INSERT INTO user_emails VALUES (?, ?, ?, ?)",
(now, 'John', 'Smith', random.randint(0, 1000)))
time.sleep(1) # so we get new timestamps
# Later on, doing some work
with sqlite3.connect('some_database.db') as conn:
c = conn.cursor()
# Get whole user history
c.execute("""SELECT * FROM user_emails
WHERE first_name = ? AND last_name = ?
""", ('John', 'Smith'))
print("All data")
for row in c.fetchall():
print(row)
print('...............................................................')
# Or, let's get the last email address
print("Latest data")
c.execute("""
SELECT * FROM user_emails
WHERE first_name = ? AND last_name = ?
ORDER BY datetime DESC
LIMIT 1;
""", ('John', 'Smith'))
print(c.fetchall())
</code></pre>
<p>注意:在这段代码中,数据检索运行得非常快,因为我使用<code>time.sleep(1)</code>生成假用户数据,所以只需要~5秒就可以运行。你知道吗</p>