[英]Generate a sorted list of birthday dates and append each date to a newline in a file
So, I have been trying to generate a wordlist with birthday dates. 因此,我一直在尝试生成带有生日日期的单词表。 I am trying to append each value to a newline in a file birdthday_wordlist.txt.
我试图将每个值附加到文件birdthday_wordlist.txt中的换行符上。 The file and the format should be like this:
文件和格式应如下所示:
01/01/1998
02/01/1998
03/01/1998
dd/mm/yyyy
12/12/2000
I was capable of generating only the dd, mm or yyyy, with scripts like this: 我只能使用以下脚本生成dd,mm或yyyy:
with open('XXXXXX_wordlist.txt', 'w') as birdthday_wordlist:
for i in range(1980, 2000):
birdthday_wordlist.write('{}\n'.format(i))
I know there is a way, for now I couldn't figure it out. 我知道有办法,现在我还不知道。
You can use a while and the datetime functions. 您可以使用一会儿和日期时间函数。 You can set the ini date as you need, and the end date you want.
您可以根据需要设置初始日期和结束日期。 It will sum 1 day each iteration
每次迭代总计1天
import datetime
ini=datetime.date(year=1980,month=1,day=1)
end=datetime.date(year=2000,month=1,day=1)
while ini<=end:
birdthday_wordlist.write(ini.strftime('%d/%m/%Y')+'\n')
ini=ini+datetime.timedelta(days=1)
If I understand what you're asking, it's very similar to the question here 如果我了解您的要求,则与此处的问题非常相似
I have adapted the answer to write the dates to a file: 我已将答案改写为将日期写入文件:
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + timedelta(n)
start_date = date(1980, 1, 1)
end_date = date(2000, 1, 1)
with open('XXXXXX_wordlist.txt', 'w+') as birdthday_wordlist:
for single_date in daterange(start_date, end_date):
birdthday_wordlist.write('%s\n' % single_date.strftime("%d/%m/%Y"))
Will output: 将输出:
01/01/1980
02/01/1980
03/01/1980
04/01/1980
05/01/1980
...
31/12/1999
If you want a solution that doesn't use imported packages, you can do the following: 如果您想要不使用导入程序包的解决方案,则可以执行以下操作:
data = []
f_string = "%02d/%02d/%04d\n" # Will be presented as dd/mm/yyyy format
for year in range(1980, 2000):
for month in range(1, 12):
for day in range(1, 31):
data.append(f_string % (day, month, year))
with open('XXXXXX_wordlist.txt', 'w') as birthday_wordlist:
for item in data:
birthday_wordlist.write(item)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.