变量不起作用...或者我遗漏了一些东西?

发布于 2024-11-07 01:09:49 字数 2336 浏览 0 评论 0原文

我正在尝试对 docx 文件进行一些编辑...将数字转换为字母(即,如果变量等于 01 = '一',等于 02 = '二',依此类推,但使用西班牙语)。问题是变量 f_dia_nom 不起作用...它甚至不打印任何内容...我做错了什么吗?或者我错过了什么?

#!/usr/bin/env python2.6

from Tkinter import *
from docx import *
import tkMessageBox

root = Tk()

nombre = ""
exp_no = ""
ubic = ""
munic = ""
prov = ""
f_dia = ""
f_dia2 = ""
f_dia_nom = ""

def nombre_dia():
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

# Hacer el docx      
def makedocx():
         if __name__ == '__main__':        
             # Default set of relationshipships - these are the minimum components of a document
             relationships = relationshiplist()

             # estructura del documento
             document = opendocx('test.docx')
             docbody = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]

             # Buscar y reemplazar
             print 'Replacing ...',
             docbody = replace(docbody,'V_EXP',en1.get())            
             docbody = replace(docbody,'V_NOMBRE',en0.get()) 
             docbody = replace(docbody,'V_OPERACION',op.get())           
             docbody = replace(docbody,'V_UBIC',en3.get())
             docbody = replace(docbody,'V_MUNI',en4.get())
             docbody = replace(docbody,'V_PROV',en5.get())
             docbody = replace(docbody,'V_F_DIA',en6.get())
             docbody = replace(docbody,'V_F_MES',mes.get())          
             docbody = replace(docbody,'V_F_SEM',sem.get())
             docbody = replace(docbody,'V_NUM_DIA',en7.get())            
             nombre_dia()
             docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)            
             print 'f_dia_nom'
             print 'done.'

I am trying to make some edits to a docx file... making a number into letter (i.e. if the variable is equal to 01 = 'one', equal to 02 = 'two', and so on, but in Spanish). The problem is that the variable f_dia_nom doesn't work ... it doesn't even print anything... am I doing something wrong?? or am I missing something??

#!/usr/bin/env python2.6

from Tkinter import *
from docx import *
import tkMessageBox

root = Tk()

nombre = ""
exp_no = ""
ubic = ""
munic = ""
prov = ""
f_dia = ""
f_dia2 = ""
f_dia_nom = ""

def nombre_dia():
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

# Hacer el docx      
def makedocx():
         if __name__ == '__main__':        
             # Default set of relationshipships - these are the minimum components of a document
             relationships = relationshiplist()

             # estructura del documento
             document = opendocx('test.docx')
             docbody = document.xpath('/w:document/w:body',namespaces=nsprefixes)[0]

             # Buscar y reemplazar
             print 'Replacing ...',
             docbody = replace(docbody,'V_EXP',en1.get())            
             docbody = replace(docbody,'V_NOMBRE',en0.get()) 
             docbody = replace(docbody,'V_OPERACION',op.get())           
             docbody = replace(docbody,'V_UBIC',en3.get())
             docbody = replace(docbody,'V_MUNI',en4.get())
             docbody = replace(docbody,'V_PROV',en5.get())
             docbody = replace(docbody,'V_F_DIA',en6.get())
             docbody = replace(docbody,'V_F_MES',mes.get())          
             docbody = replace(docbody,'V_F_SEM',sem.get())
             docbody = replace(docbody,'V_NUM_DIA',en7.get())            
             nombre_dia()
             docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)            
             print 'f_dia_nom'
             print 'done.'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远昼 2024-11-14 01:09:49

查找 python 的“全局范围”规则。一般来说,尽量避免

在 nombre_dia() 中使用全局变量(如果只是为了避免这样的错误):

f_dia_nom = "Un" 

或者与此相关的任何其他赋值,都会让 python 编译器在 nombre_dia 函数的本地创建该数字。

修改您的函数以将 f_dia_nom 声明为全局函数:

def nombra_dia():
   global f_dia_nom

   ...

它将有 nombre_dia 请参阅 f_dia_nom 作为全局

函数 参考

学习 Python 第 4 版第 408 页

Look up 'global scoping' rules for python. In general, try to avoid global variables as much as possible(if only to avoid errors like these)

in nombre_dia():

f_dia_nom = "Un" 

or any other assignment for that matter has the python compiler create that number LOCAL to the nombre_dia function.

modify your function to declare f_dia_nom as a global:

def nombra_dia():
   global f_dia_nom

   ...

It will have nombre_dia SEE the f_dia_nom as the global

References

Learning Python 4th edition pg 408

○闲身 2024-11-14 01:09:49

f_dia_nom 是一个全局变量,这并不是一个好的开始。如果您想操作它,请将 f_dia2 作为参数传递给函数 nombre_dia(),然后从函数返回 f_dia_nom

def nombre_dia(f_dia2):
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

         return f_dia_nom

if __name__ == '__main__':
    # All your other code...
    f_dia_nom = nombre_dia(f_dia2)
    print 'f_dia_nom =', f_dia_nom
    docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)
    print 'done.'

f_dia_nom is a global variable, which is not really a great start. If you want to manipulate it, pass f_dia2 to the function nombre_dia() as an argument and then return f_dia_nom from the function afterward.

def nombre_dia(f_dia2):
         if f_dia2 == 1 or f_dia2 == 01:        
             f_dia_nom = "Un"
         elif f_dia2 == 2 or f_dia2 == 02:               
             f_dia_nom  =  "Dos"
         elif f_dia2 == 3 or f_dia2 == 03:        
             f_dia_nom = "Tres"
         elif f_dia2 == 4 or f_dia2 == 04:               
             f_dia_nom = "Cuatro"
         elif f_dia2 == 5 or f_dia2 == 05:               
             f_dia_nom = "Cinco"
         elif f_dia2 == 6 or f_dia2 == 06:        
             f_dia_nom = "Seis"
         elif f_dia2 == 7 or f_dia2 == 07:               
             f_dia_nom = "Siete"             
         else:
             f_dia_nom = "Error"

         return f_dia_nom

if __name__ == '__main__':
    # All your other code...
    f_dia_nom = nombre_dia(f_dia2)
    print 'f_dia_nom =', f_dia_nom
    docbody = replace(docbody,'V_NOM_DIA',f_dia_nom)
    print 'done.'
汹涌人海 2024-11-14 01:09:49

去掉 if __name__ == '__main__': 那是测试你是否在主模块中。 (您特别要求运行的那个)。我猜你不是。

if __name__ == '__main__' 仅当您处于模块级别时才有意义(在任何函数之外,因此您可以检测您是否正在运行或正在导入。)

Get rid of if __name__ == '__main__': That is testing whether or not you are in the main module. (The one you specifically asked to run). I'm guessing that you aren't.

if __name__ == '__main__' only makes sense if you are on the module level (outside of any functions, so you can detect if you are being run or being imported.)

胡大本事 2024-11-14 01:09:49

很难准确地看出代码中应该发生什么,但有几点:

  1. 您正在打印字符串“f_dia_nom”而不是变量f_dia_nom

  2. 如果不首先使用 global,则无法从函数内部更改全局变量

  3. 无需检查 1 和 01(等),因为 1 == 01

  4. 您似乎没有在任何地方设置/创建 f_dia2...

Difficult to see exactly what's supposed to be going on in your code, but a few points:

  1. You're printing the string 'f_dia_nom' rather than the variable f_dia_nom

  2. You can't change global variables from inside a function without using global <variable> first

  3. No need to check against 1 and 01 (etc.) since 1 == 01

  4. You don't seem to be setting/creating f_dia2 anywhere...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文