3

I need to join two strings.

The first string = dates:

(MegaMillions2019 = (date.strftime("%m%d%Y")))

The second string = results:

(results = '\n'.join([', '.join(parsed[i]) for i in range(len(parsed))])
(results.replace(' ','')))

These strings need to be on the same line shown below.

Code:

import requests
from bs4 import BeautifulSoup
from datetime import datetime

response = requests.get('https://www.lotterycorner.com/mi/mega-millions/2019')
soup = BeautifulSoup(response.text, 'html.parser')
date = soup.find_all("td", {"class":"win-nbr-date col-sm-3 col-xs-4"})
for date in date:
    date2 = (date.get_text())
    date = (datetime.strptime(date2, '%b %d, %Y'))
    MegaMillions2019 = (date.strftime("%m%d%Y"))
    print(MegaMillions2019)

data = []
for ultag in soup.find_all("ul",{"class":"nbr-grp"}):
    for litag in ultag.find_all('li'):
        results = (litag.get_text().replace(' ','').replace('MegaBall',''))
        data.append(results)

parsed = []
for i in range(int(len(data)/7)):
    j = i*7
    parsed.append(data[j:j+6])

results = '\n'.join([', '.join(parsed[i]) for i in range(len(parsed))])
print(results.replace(' ',''))

Output dates:

01222019
01182019
01152019
01112019
01082019
01042019
01012019

Results:

8,16,30,38,61,10
4,15,37,59,64,16
2,43,48,62,64,24
29,52,58,60,62,7
4,5,31,62,69,20
13,26,29,38,64,5
21,29,35,54,60,15

I would like them to be joined like this:

01222019,8,16,30,38,61,10
01182019,4,15,37,59,64,16
01152019,2,43,48,62,64,24
01112019,29,52,58,60,62,7
01082019,4,5,31,62,69,20
01042019,13,26,29,38,64,5
01012019,21,29,35,54,60,15
marchoy
  • 117
  • 1
  • 8

4 Answers4

0

You can do the following

MegaMillions2019 =[]
for date in date:
    date2 = (date.get_text())
    date = (datetime.strptime(date2, '%b %d, %Y'))
    MegaMillions2019.append(date.strftime("%m%d%Y")))
...

for mm, r in zip(MegaMillions2019, results.replace(' ','')): 
    print (f"{mm}, {r}")
Jibin Mathews
  • 1,129
  • 1
  • 10
  • 23
0

keep two lists l1 and l2, append dates in l1 in first loop and append list of results in l2 inside second loop

example:

l1=[01222019,01222029,01222013] l2=[[3,4,5],[1,2,3],[4,5,5]]

then you can zip both lists as follows:

new_list = [",".join(map(str, (list(a) + b))) for a, b in zip(s1, s2)]

now you can print new_list which will have comma separated results.

kalimba
  • 408
  • 4
  • 14
0
dates = []
for date in date:
    ...
    dates.append(str(MegaMillions2019))

...

parsed = []
joined = []
for i in range(int(len(data)/7)):
    j = i*7
    parsed.append(data[j:j+6])
    parsedline = [', '.join(parsed[j]) for j in range(len(parsed))][i]
    joined.append(dates[i]+', '+ parsedline)

results = '\n'.join(joined)
print(results.replace(' ',''))

Demo: https://repl.it/@glhr/55449729

Output sample:

03292019,5,14,15,62,66,3
03262019,4,14,22,43,58,9
glhr
  • 4,439
  • 1
  • 15
  • 26
0

Use a separate list to store the dates, and print the elements using zip_longest(), in case if any elem of the list is missing it would fill it with None instead of missing the element:

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

Hence:

import requests
from bs4 import BeautifulSoup
from datetime import datetime

response = requests.get('https://www.lotterycorner.com/mi/mega-millions/2019')
soup = BeautifulSoup(response.text, 'html.parser')
date = soup.find_all("td", {"class":"win-nbr-date col-sm-3 col-xs-4"})
megaList = []                                    # empty list for dates
for date in date:
    date2 = (date.get_text())
    date = (datetime.strptime(date2, '%b %d, %Y'))
    MegaMillions2019 = (date.strftime("%m%d%Y"))
    megaList.append(MegaMillions2019)

data = []
for ultag in soup.find_all("ul",{"class":"nbr-grp"}):
    for litag in ultag.find_all('li'):
        results = (litag.get_text().replace(' ','').replace('MegaBall',''))
        data.append(results)

parsed = []
for i in range(int(len(data)/7)):
    j = i*7
    parsed.append(data[j:j+6])

results = [', '.join(parsed[i]) for i in range(len(parsed))]
results = [i.replace(' ','') for i in results]     # remove space from each elem in results

for mg, res in zip_longest(megaList, results):
print(mg, res, sep =',')

OUTPUT:

03292019,5,14,15,62,66,3
03262019,4,14,22,43,58,9
.
.
01252019,8,16,30,38,61,10
01222019,4,15,37,59,64,16
01182019,2,43,48,62,64,24
.
.
01042019,21,29,35,54,60,15
01012019,34,44,57,62,70,14
DirtyBit
  • 16,613
  • 4
  • 34
  • 55