从QRegExp unicode值替换点和逗号

时间:2017-01-28 03:24:38

标签: python python-2.7 pyqt pyqt4

我有一个unicode值,它源自这种验证器:

# numbers with dots and comma
validator= QtGui.QRegExpValidator(QtCore.QRegExp("^\\d{1,3}(([.]\\d{3})*),(\\d{2})$"))

我需要在此值中替换所有逗号并将其转换为int

我试图制作:

# the replace.replace below i used to use in c++
newvalue = int(oldvalue.replace(".","").replace(",",""))

但它仅适用于以下值:1.000.000,000

在0,00或500.000,00之类的值中我有此错误消息:

The debugged program raised the exception unhandled AttributeError "'NoneType' object has no attribute 'replace'"
有人可以帮我解决这个问题吗? 非常感谢你!

1 个答案:

答案 0 :(得分:0)

好的,

问题是在某些情况下,oldvalue == None,所以我收到了错误消息。

通过简单的验证解决了问题:

if self.rec1[5] != None:
    newvalue = int(oldvalue.replace(".","").replace(",",""))