Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
375 views
in Technique[技术] by (71.8m points)

python - 如何在python中将HTML文件作为字符串存储在变量中(How to store a html file in a variable as a string in python)

I have html file and I want to store the contents of that file in a variable as a string value.

(我有html文件,我想将该文件的内容存储在变量中作为字符串值。)

I have tried this but it returns a blank string.

(我已经尝试过了,但是它返回一个空字符串。)

How can I do this?

(我怎样才能做到这一点?)

def myfunc():
    str = ""
    with open("/home/suman/Alert_logo_report.html") as report_file:
        raw_html = report_file.readlines()
        str = ''.join(raw_html)
    return str
  ask by SumanKalyan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use """string_literal""" instead of "string_literal" or 'string_literal' .

(使用"""string_literal"""代替"string_literal"'string_literal' 。)

"""string_literal""" is for multi line strings.

(“”“ string_literal”“”用于多行字符串。)

This will work.

(这将起作用。)

def myfunc():
str = """"""
with open("employees.html") as report_file:
    raw_html = report_file.readlines()
    str = """""".join(raw_html)
return str

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...