如何连接两个单独的字符串

时间:2019-04-01 07:10:44

标签: python join web-scraping formatting

我需要连接两个字符串。

第一个字符串=日期:

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

第二个字符串=结果:

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

这些字符串必须在下面显示的同一行上。

代码:

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(' ',''))

输出日期:

01222019
01182019
01152019
01112019
01082019
01042019
01012019

结果:

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

我希望他们像这样加入:

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

4 个答案:

答案 0 :(得分:0)

您可以执行以下操作

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}")

答案 1 :(得分:0)

保留两个列表'(cities)'l1,在第一个循环中将日期附加在l2中,在第二个循环中将结果列表附加在l1

示例:

l2

然后您可以按如下所示压缩两个列表:

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

现在您可以打印将以逗号分隔的结果的new_list。

答案 2 :(得分: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(' ',''))

演示:https://repl.it/@glhr/55449729

输出样本:

03292019,5,14,15,62,66,3
03262019,4,14,22,43,58,9

答案 3 :(得分:0)

使用单独的列表存储日期,并使用zip_longest()打印元素,以防万一缺少列表中的任何元素时,将其填充None而不是丢失元素:< / p>

  

制作一个迭代器,该迭代器汇总每个可迭代对象中的元素。   如果可迭代项的长度不均匀,则填写缺失值   具有fillvalue。迭代一直持续到最长的迭代是   精疲力尽。

因此

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 =',')

输出

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