我正在用 Python 解析文本,我有这个最终代码来写句子,但效果不佳:
opt = child.get('desc')
extent = child.get('extent')
if opt == 'es':
opt = "ESP:"
elif opt == "la":
opt = "LAT:"
elif opt == "en":
opt = "ENG:"
if opt in ["es","la","en","ar","fr"] and extent == "begin":
print time, opt+(" " + opt).join([c.encode('latin-1') for c in child.tail.split(' ')])
它只适用于 OR 语句,但是当我添加 AND 语句(我确实需要)时,没有任何变化。有人要吗?
请您参考如下方法:
除非第一行代码的输出是 "ar" 或 "fr"(或者 if-elif 中没有的其他内容> 条件),您正在覆盖 opt 变量。考虑将"new"opt 重命名为其他名称,如下所示:
opt = child.get('desc')
extent = child.get('extent')
if opt == 'es':
opt2 = "ESP:"
elif opt == "la":
opt2 = "LAT:"
elif opt == "en":
opt2 = "ENG:"
# Check variable values
print "opt: ", opt
print "opt2: ", opt2
if opt in ["es","la","en","ar","fr"] and extent == "begin":
print time, opt2+(" " + opt2).join([c.encode('latin-1') for c in child.tail.split(' ')])
我不确定你到底想从代码中实现什么,但如果原始 child.get('desc' ) 条件返回列表中存在的字符串。
