变量不起作用...或者我遗漏了一些东西?
我正在尝试对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
查找 python 的“全局范围”规则。一般来说,尽量避免
在 nombre_dia() 中使用全局变量(如果只是为了避免这样的错误):
或者与此相关的任何其他赋值,都会让 python 编译器在 nombre_dia 函数的本地创建该数字。
修改您的函数以将 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():
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:
It will have nombre_dia SEE the f_dia_nom as the global
References
Learning Python 4th edition pg 408
f_dia_nom
是一个全局变量,这并不是一个好的开始。如果您想操作它,请将f_dia2
作为参数传递给函数nombre_dia()
,然后从函数返回f_dia_nom
。f_dia_nom
is a global variable, which is not really a great start. If you want to manipulate it, passf_dia2
to the functionnombre_dia()
as an argument and then returnf_dia_nom
from the function afterward.去掉
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.)很难准确地看出代码中应该发生什么,但有几点:
您正在打印字符串“f_dia_nom”而不是变量f_dia_nom
如果不首先使用
global
,则无法从函数内部更改全局变量无需检查 1 和 01(等),因为 1 == 01
您似乎没有在任何地方设置/创建
f_dia2
...Difficult to see exactly what's supposed to be going on in your code, but a few points:
You're printing the string 'f_dia_nom' rather than the variable f_dia_nom
You can't change global variables from inside a function without using
global <variable>
firstNo need to check against 1 and 01 (etc.) since 1 == 01
You don't seem to be setting/creating
f_dia2
anywhere...