Python javascript 生成器
Python javascript builder
我正在尝试使用 Python 为 Javascript 代码创建一个生成器。我已经尝试了所有我能想到的方法,但我仍然遇到语法错误。
错误是这样的:
文件 "test.py",第 12 行
exploit = ("var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome
"+word.split('').reverse().join('')+" 等同于 "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome
"+word.split('').reverse().join('')+" 与 "+word)}}pal();" 不同
^
语法错误:语法无效
我想将 ("javascript code") 转换为字符串,但建议不起作用?谢谢抱歉,如果我的问题不清楚
我的代码:
import time as t
from os import path
def createFile(dest):
date=t.localtime(t.time())
##Filename=month+date+year
name="%d_%d_%d.js"%(date[1],date[2],(date[0]%100))
exploit = ("var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();")
s = str(exploit)
if not(path.isfile(dest+name)):
f=open(dest+name,'w')
f.write(s)
f.close()
if __name__=='__main__':
createFile("lol")
raw_input("done!!!")
一方面,您需要转义要分配给 exploit
的 javascript 字符串中的引号。或者,您可以使用三引号字符串,这更容易:
explioit = '''var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();'''
这解决了这个问题。另请注意,您不需要 s = str(exploit)
- exploit
已经是一个字符串。
另外,您的缩进似乎在函数中关闭,在这种情况下不是语法错误,但您的函数将无法按预期工作。这是一些清理后的代码:
import time
from os import path
def createFile(dest):
date = time.localtime()
##Filename=month+date+year
name = "%d_%d_%d.js" % (date[1], date[2], (date[0]%100))
exploit = '''var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();'''
if not(path.isfile(dest+name)):
with open(dest+name,'w') as f:
f.write(exploit)
if __name__=='__main__':
createFile("lol")
raw_input("done!!!")
我正在尝试使用 Python 为 Javascript 代码创建一个生成器。我已经尝试了所有我能想到的方法,但我仍然遇到语法错误。 错误是这样的:
文件 "test.py",第 12 行
exploit = ("var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome
"+word.split('').reverse().join('')+" 等同于 "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome
"+word.split('').reverse().join('')+" 与 "+word)}}pal();" 不同
^
语法错误:语法无效
我想将 ("javascript code") 转换为字符串,但建议不起作用?谢谢抱歉,如果我的问题不清楚
我的代码:
import time as t
from os import path
def createFile(dest):
date=t.localtime(t.time())
##Filename=month+date+year
name="%d_%d_%d.js"%(date[1],date[2],(date[0]%100))
exploit = ("var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();")
s = str(exploit)
if not(path.isfile(dest+name)):
f=open(dest+name,'w')
f.write(s)
f.close()
if __name__=='__main__':
createFile("lol")
raw_input("done!!!")
一方面,您需要转义要分配给 exploit
的 javascript 字符串中的引号。或者,您可以使用三引号字符串,这更容易:
explioit = '''var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();'''
这解决了这个问题。另请注意,您不需要 s = str(exploit)
- exploit
已经是一个字符串。
另外,您的缩进似乎在函数中关闭,在这种情况下不是语法错误,但您的函数将无法按预期工作。这是一些清理后的代码:
import time
from os import path
def createFile(dest):
date = time.localtime()
##Filename=month+date+year
name = "%d_%d_%d.js" % (date[1], date[2], (date[0]%100))
exploit = '''var word=prompt("Give a word","");function pal(){if(word===word.split('').reverse().join('')){document.write("hello this is a palindrome<br>"+word.split('').reverse().join('')+" is the same as "+word)}else{document.write("Error 504(Not a palindrome)...hello this is not a palindrome<br>"+word.split('').reverse().join('')+" is not the same as "+word)}}pal();'''
if not(path.isfile(dest+name)):
with open(dest+name,'w') as f:
f.write(exploit)
if __name__=='__main__':
createFile("lol")
raw_input("done!!!")