我刚刚开始学习函数,我了解基本知识,但是我不知道如何在更复杂的脚本中调用它们(如下所示)。你知道吗
下面的示例脚本尝试从MySQL数据库中提取足球比赛结果,提取到CSV,然后从CSV导入并放入HTML表中并作为电子邮件发送。我遇到的问题是,我需要将每个函数的输出(例如英超联赛、甲级联赛、乙级联赛)编译成一个报告表-我使用“report”来实现:
report = ""
report += htmlpremleague
report += "<br><br>"
report += htmlleagueone
report += "<br><br>"
return report
这是加入表,所以我可以在一封电子邮件中作为一个HTML表发送所有的结果。我遇到的问题是'report'无法定位HTML的,例如'htmlpremleague',因为它在一个函数中。但我不能简单地用"report += premier_league()"
替换它,因为该函数中还有其他内容,而不仅仅是html。理想情况下,我想让函数工作,因为(a)学习它们很有趣!和(b)这将是很酷的,能够运行每一个单独的联赛自己,例如,生成一份报告完全英超联赛。下面的示例脚本运行良好,但是这是由于报表html部分是全局的,并且位于函数之外。理想情况下,我希望他们在里面,所以我可以实现上述。你知道吗
我是新来的,我试着坚持原则,但请让我知道,如果我做了什么不正确的,或者我可以提供任何进一步的。你知道吗
重申一下,期望的输出将是所有内容都在一个可调用函数中,例如,我是否将'report'/每个表作为一个全局变量?你知道吗
def league_one():
query = "SELECT footballteam as ftt, goals, points FROM
footballdbl_league_one plfdb ORDER BY point desc"
leagueonecursor.execute(query)
leagueonerows = leagueonecursor.fetchall()
with open('league_one.csv', 'wb') as file2:
wr = csv.writer(file2, quoting=csv.QUOTE_ALL)
wr.writerow(["Team", "Goals", "Points"])
wr.writerows(leagueonerows)
print ("csv2 created")
textleagueone = """
"""
htmlleagueone = """
<html><body><p><u><h3><b>Highest Run Time</b></h3></u></p>
{table2}
<br><br>
</body></html>
"""
with open('league_one.csv') as input_file2:
reader = csv.reader(input_file2)
leagueonereader = list(reader)
textleagueone =
textleagueone.format(table2=tabulate(leagueonereader,
headers="firstrow", tablefmt="grid"))
htmlleagueone =
htmlleagueone.format(table2=tabulate(leagueonereader,
headers="firstrow", tablefmt="html"))
/Main足球脚本示例:
con = pymysql.connect(user='',password='',host='',database='')
cursor = con.cursor()
# probably not necessary
premleaguecursor = con.cursor()
leagueonecursor = con.cursor()
def premier_league():
query = "SELECT footballteam as ftt, goals, points FROM footballdbl_prem_league plfdb ORDER BY point desc"
premleaguecursor.execute(query)
premleaguerows = premleaguecursor.fetchall()
with open('prem_league.csv', 'wb') as file1:
wr = csv.writer(file1, quoting=csv.QUOTE_ALL)
wr.writerow(["Team", "Goals", "Points"])
wr.writerows(premleaguerows)
print ("premier league csv created")
textpremleague = """
"""
htmlpremleague = """
<html><body><p><font size="+1"><u><h3><b>Premier League</b></h3></u></font></p>
{table1}
</body></html>
"""
with open('prem_league.csv') as input_file1:
reader = csv.reader(input_file1)
premleaguereader = list(reader)
textpremleague = textpremleague.format(table1=tabulate(premleaguereader, headers="firstrow", tablefmt="grid"))
htmlpremleague = htmlpremleague.format(table1=tabulate(premleaguereader, headers="firstrow", tablefmt="html"))
def league_one():
query = "SELECT footballteam as ftt, goals, points FROM footballdbl_league_one plfdb ORDER BY point desc"
leagueonecursor.execute(query)
leagueonerows = leagueonecursor.fetchall()
with open('league_one.csv', 'wb') as file2:
wr = csv.writer(file2, quoting=csv.QUOTE_ALL)
wr.writerow(["Team", "Goals", "Points"])
wr.writerows(leagueonerows)
print ("csv2 created")
textleagueone = """
"""
htmlleagueone = """
<html><body><p><u><h3><b>Highest Run Time</b></h3></u></p>
{table2}
<br><br>
</body></html>
"""
with open('league_one.csv') as input_file2:
reader = csv.reader(input_file2)
leagueonereader = list(reader)
textleagueone = textleagueone.format(table2=tabulate(leagueonereader, headers="firstrow", tablefmt="grid"))
htmlleagueone = htmlleagueone.format(table2=tabulate(leagueonereader, headers="firstrow", tablefmt="html"))
# def league_two():
# def league_three():
def report():
report = ""
report += htmlpremleague
report += "<br><br>"
report += htmlleagueone
report += "<br><br>"
return report
def send_email():
report()
me = ''
password = ''
server = ''
you = ''
message = MIMEMultipart(
"alternative", None, [MIMEText(report, 'html')])
message['Subject'] = "FOOTBALL REPORT"
message['From'] = me
message['To'] = you
server = smtplib.SMTP(server)
server.ehlo()
server.starttls()
server.login(me, password)
server.sendmail(me, you, message.as_string())
server.quit()
def main():
premier_league()
league_one()
report()
send_email()
只需在
def
中向它提供所需的数据—我选择作为列表来执行,您也可以像这样依次传递它def report2(myOne, myTwo): ...
并将其称为report2(one,two)
—无需为此使用globals。你知道吗输出:
相关问题 更多 >
编程相关推荐