How To Join Two Seperate Strings
Solution 1:
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
Solution 2:
You can do the following
MegaMillions2019 =[]
fordateindate:
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}")
Solution 3:
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 inzip(s1, s2)]
now you can print new_list which will have comma separated results.
Solution 4:
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 datesfor 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 inrange(int(len(data)/7)):
j = i*7
parsed.append(data[j:j+6])
results = [', '.join(parsed[i]) for i inrange(len(parsed))]
results = [i.replace(' ','') for i in results] # remove space from each elem in resultsfor 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
Post a Comment for "How To Join Two Seperate Strings"