我有这个python脚本,我宁愿使用JavaScrip

2022-10-02 00:50:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一大块Python,我从各种来源拼凑而成(我不是程序员或开发人员,甚至不从事任何技术行业,我只需要一些东西来自动化一些过程)。这不是实际的特定脚本,但它的编写和工作方式相同:

import os
os.system('cls')

from sys import argv

script, filename = argv

print "MADLIBS!"

print "If %r exists, it will be erased." % filename
print "To abort, hit CTRL-C (^C)."
print "To proceed, hit RETURN."

raw_input("?")

os.system('cls')

print "Opening file %r..." % filename
target = open(filename, 'w')

print "Overwriting file %r." % filename
target.truncate()

analyst = raw_input("Please enter the title of this madlib: ")
appdate = raw_input("Please enter your name: ")
print

print "Thank you.  Please enter the following information:"
print

verb    = raw_input("   Enter a verb:           ")
noun    = raw_input("   Enter a noun:           ")


print "Thank you."


target.write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>")
target.write("\n")
target.write("<html xml:lang='en' xmlns='http://www.w3.org/1999/xhtml' lang='en'>")
target.write("\n")
target.write("<head>")
target.write("\n")
target.write("<meta http-equiv='content-type' content='text/html; charset=UTF-8'/>")
target.write("\n")
target.write(appdate)
target.write("'s Madlib story!")
target.write("<p>")
target.write("Go ")
target.write(verb)
target.write(" a ")
target.write(noun)
target.write(".")

target.write("<p>")
target.write("<p>")
target.write("<p>")

target.write("<a href='javascript:window.print()' title='Send to printer'>Send to printer</a>&nbsp;&nbsp;")
target.write("\n")
target.write("")
target.write("\n")
target.write("<script language='VBScript'>")
target.write("\n")
target.write("    Sub Window_Onload")
target.write("\n")
target.write("      window.opener = 'x'")
target.write("\n")
target.write("    End Sub")
target.write("\n")
target.write("</script>")
target.write("\n")
target.write("")
target.write("\n")
target.write("\n")

target.write("<a href='javascript:self.close()' title='Close this window'>Close this window</a>")
target.write("\n")

target.write("</body>")
target.write("\n")
target.write("</html>")

target.close()
os.system(filename)

它使用从用户通过Python收集的输入创建一个HTML文档。它基本上实现了我希望它做的事情,但我开始认为Python并不是实现我想要的东西的最佳途径(我之所以创建这个脚本,是因为我对Python很好奇,想尝试创建一些东西)。对于使用JavaScript(而不是Python)在浏览器窗口中收集用户输入并将其显示到浏览器窗口中的类似操作,有人能给出一般性建议吗?你知道吗

我意识到代码的组合方式有一定程度的笨拙,也不是特别优雅,但这是我以后可以处理的。现在,我只想让它的JavaScript版本正常工作,以便以后可以调整和完善它。有什么建议吗?你知道吗


Tags: targetinputrawtitleoshtmlscriptfilenamewindowthissystemwriteprintenterplease
1条回答
网友
1楼 ·

这和JavaScript一样基本。下面是一段代码的粗略翻译。你知道吗

var appdate = prompt("Please enter your name:"); var verb = prompt("Enter a verb:"); var noun = prompt("Enter a noun:"); var story = appdate + "'s Madlib story!<p>Go " + verb + " a " + noun + "."; document.getElementById("madLib").innerHTML = story; ; ^{pr2}$ ;