如何将 html 文件导入 python 变量?
How to import html file into python variable?
我正在尝试使用 python、MIMEMultipart 和 smtp 发送 html 电子邮件。 html 很长,所以我将所有 html 放在一个 html 文件中。现在我想将 html 文件导入我的 python 文件(在同一文件夹中)并将所有 html 设置为一个名为 python 的变量中的字符串 html_string.
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html>
如何将所有 html 导入我的 python 文件并将其设置为变量:
python:
html_string = """
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html> """
您可以打开文件,读取其所有内容,并将其设置为一个变量:
with open('html_file.html', 'r') as f:
html_string = f.read()
只需像在任何 I/O 中一样打开文件。
with open('test.html', 'r') as f:
html_string = f.read()
您可以简单地使用以下内容
fname = "FILE.html"
html_file = open(fname, 'r', encoding='utf-8')
source_code = html_file.read()
print(source_code)
它会打印整个 .html 文件,如下面在 Spyder
中截取的屏幕截图所示
我正在尝试使用 python、MIMEMultipart 和 smtp 发送 html 电子邮件。 html 很长,所以我将所有 html 放在一个 html 文件中。现在我想将 html 文件导入我的 python 文件(在同一文件夹中)并将所有 html 设置为一个名为 python 的变量中的字符串 html_string.
html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html>
如何将所有 html 导入我的 python 文件并将其设置为变量:
python:
html_string = """
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Welcome to infinity2o</title>
<style type="text/css">
body {
padding-top: 0 !important;
padding-bottom: 0 !important;
padding-top: 0 !important;
padding-bottom: 0 !important;
margin: 0 !important;
width: 100% !important;
-webkit-text-size-adjust: 100% !important;
-ms-text-size-adjust: 100% !important;
-webkit-font-smoothing: antialiased !important;
}
.
.
.
</head>
</html> """
您可以打开文件,读取其所有内容,并将其设置为一个变量:
with open('html_file.html', 'r') as f:
html_string = f.read()
只需像在任何 I/O 中一样打开文件。
with open('test.html', 'r') as f:
html_string = f.read()
您可以简单地使用以下内容
fname = "FILE.html"
html_file = open(fname, 'r', encoding='utf-8')
source_code = html_file.read()
print(source_code)
它会打印整个 .html 文件,如下面在 Spyder
中截取的屏幕截图所示