0

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. 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:

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
coredumped0x
  • 768
  • 1
  • 12
  • 28
  • If you need to loop through ranges in parallel, use `zip`, although this won't account for months with a different number of days – user3483203 Jun 28 '18 at 15:05
  • 1
    Apologies, I can't understand what you're asking. Where are you trying to sort values here? – roganjosh Jun 28 '18 at 15:07
  • 1
    Please add an example of the original file that you have, it's hard to understand what you are asking since you only showed the result file. – Oran Jun 28 '18 at 15:09
  • Thank you guys for your time, and I'm sorry if I couldn't make my question more clear. I just had an answer ^^ – coredumped0x Jun 28 '18 at 15:25

3 Answers3

0

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

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)
nacho
  • 5,280
  • 2
  • 25
  • 34
  • And how is that sorted in any way? – roganjosh Jun 28 '18 at 15:12
  • I believe OP wants leading zeroes, i.e. 01/01/1980, 02/01/1980, etc. – natn2323 Jun 28 '18 at 15:14
  • Yeah, but you haven't actually sorted the list at all, and the question title asks for sorting. – roganjosh Jun 28 '18 at 15:14
  • 1
    With this format he would get leading zeros '%d/%m/%Y' – nacho Jun 28 '18 at 15:14
  • I'm not talking about leading zeros, I'm talking about _sorting_ the data – roganjosh Jun 28 '18 at 15:16
  • @roganjosh No, he didn't say anything about sorting. He asked for a sorted list and that is what he gets. It is not the same to get a sorted list than to sort a list. – nacho Jun 28 '18 at 15:16
  • 1
    I honestly don't get the distinction, perhaps it's time for me to eat humble pie. Please explain what I'm not seeing. – roganjosh Jun 28 '18 at 15:19
  • @roganjosh It's easy. The list you get it is already sorted because you begin in the first day, and you add 1 day each time. So you get a sorted list and that is what he needs. – nacho Jun 28 '18 at 15:21
  • There is nothing in the question content that suggests this is sorted beforehand. The title of the question asked for "sorted" and we don't know how the data is generated. – roganjosh Jun 28 '18 at 15:25
  • The data is "sorted" by day, then month, then year. The data isn't being generated from an external source... it's just being created by the user and written to a file. – natn2323 Jun 28 '18 at 15:27
  • Yep, humble pie time :P – roganjosh Jun 28 '18 at 15:31
  • 1
    Mr. @nacho thank you for your time and contribution. The thing with your code is when I ran it, I had a 1.8 GB file with a couple of tens of millions of lines with the only value 01/01/1980 in all of that in every line...my computer's memory overflowed and I had to force quit everything and restart the machine. – coredumped0x Jun 28 '18 at 18:50
0

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
Ethan Brews
  • 131
  • 1
  • 5
0

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)
natn2323
  • 1,983
  • 1
  • 13
  • 30
  • 1
    @musikrek much appreciated and thank you for your time and help. I just noticed that your code resulted in a Traceback (most recent call last): File "new.py", line 10, in birthday_wordlist.write(data) TypeError: expected a string or other character buffer object So I just str(data) and It worked but I get many dates at one line in this format: '13/01/1980\n', '14/01/1980\n', '15/01/1980\n', '16/01/1980\n', '17/01/1980\n', '18/01/1980\n', ... – coredumped0x Jun 28 '18 at 18:58
  • My apologies. The error was that I was writing the file, which makes no sense. Instead, I *should've* been writing the items in the `data` variable, as I am now doing. – natn2323 Jun 28 '18 at 20:53