def make_pdf(self):
        self.get_filez()
        self.get_client()
        file_name = self.client_id+"_"+self.client_name+"_"+self.batch_num
        style = libxslt.parseStylesheetDoc(self.xsl_file)
        transformation = style.applyStylesheet(self.xml_file,None)
        style.saveResultToFilename("tmp/"+file_name+".fo",transformation,0)
        style.freeStylesheet()
        self.xml_file.freeDoc()
        transformation.freeDoc()
        fop_cmd = "/usr/bin/xmlgraphics-fop"
        #file_name = self.tpa+"_"+self.be+"_"+self.batch_num
        cmd = [fop_cmd,"-fo","tmp/"+file_name+".fo","-pdf","tmp/"+file_name+".pdf"]
        #fop_transform = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
        #fop_log = "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"
        #fop_log = fop_log + time.strftime('%Y-%m-%d %R:%S')+"\n"
        #fop_log = fop_log + file_name+".fo" + "\n"
        #fop_log =  fop_transform.communicate()[0]+"\n"
        #f = open("/tmp/error_log","a")
        #f.write(fop_log)
        #f.close()


确定如果我注释掉cmd变量声明,代码将运行并正确创建fo文件。与上面一样,它没有注释,我在未定义的file_name上得到NameError(位于顶部)。如果我取消注释在cmd声明上方的第二个file_name声明,它将在自身上引发NameError。 '。'在过去,当发生这种情况时,这是一种语法错误。我想念它,请帮助!

当file_name的第二个声明被注释掉时:

Traceback (most recent call last):
  File "make_pdfs.py", line 11, in ?
    from MakePdfs import MakePdfs
  File "/home/khouser/removed/removed/MakePdfs.py", line 16, in ?
    class MakePdfs:
  File "/home/khouser/removed/removed/MakePdfs.py", line 39, in MakePdfs
    cmd = [fop_cmd,"-fo","tmp/"+file_name+".fo","-pdf","tmp/"+file_name+".pdf"]
NameError: name 'file_name' is not defined


当第二个file_name声明未注释时:

Traceback (most recent call last):
  File "make_pdfs.py", line 11, in ?
    from MakePdfs import MakePdfs
  File "/home/khouser/removed/removed/MakePdfs.py", line 16, in ?
    class MakePdfs:
  File "/home/khouser/removed/removed/MakePdfs.py", line 38, in MakePdfs
    file_name = self.tpa+"_"+self.be+"_"+self.batch_num
NameError: name 'self' is not defined

最佳答案

您的文件包含不可见的控制字符可能会引起神秘的NameErrors。在UNIX机器上,您可以通过查看以下命令的输出来发现这些错误:

cat -A filename.py

关于python - 最明确定义var时的Python NameError,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5756777/

10-11 19:37