将字符串导出为HTML文件

时间:2013-09-20 07:44:10

标签: javascript python-2.7

尝试将字符串写入HTML文件,但在第三行'pagetitle'上出现语法错误。我怎样才能正确导出这个? python的新手和我的笔记没有帮助。

用Python编写,导出为HTML。

def paragraph_function(filename, pagetitle, textbody):
    output_file = file(filename)
    output_file.write('<html><head><title>'pagetitle'</title></head>')

2 个答案:

答案 0 :(得分:1)

您应该将 pagetitle 连接到正在写入输出文件的字符串

output_file.write('<html><head><title>' + pagetitle + '</title></head>')

答案 1 :(得分:0)

试试这个:

def paragraph_function(filename, pagetitle, textbody):
  output_file = open(filename,'w+')
  data = '<html><head><title>'+pagetitle+'</title></head>'
  output_file.write(data)


pagetitle= 'page'
filename= 'out'
textbody = ''
paragraph_function(filename, pagetitle, textbody)