-3

我只是想修改一个字符串并返回修改后的字符串,但是,在打印变量时我得到“无”返回。

def AddToListTwo(self,IndexPosition):
    filename = RemoveLeadingNums(self, str(self.listbox1.get(IndexPosition))) #get the filename, remove the leading numbers if there are any
    print filename #this prints None
    List2Contents = self.listbox2.get(0, END)
    if(filename not in List2Contents): #make sure the file isn't already in list 2
        self.listbox2.insert(0, filename)

def RemoveLeadingNums(self, words):
    if(isinstance(words,str)):
        match = re.search(r'^[0-9]*[.]',words)
        if match: #if there is a match, remove it, send it through to make sure there aren't repeating numbers
            RemoveLeadingNums(self, re.sub(r'^[0-9]*[.]',"",str(words)).lstrip())
        else:
            print words #this prints the value correctly
            return words
    if(isinstance(words,list)):
        print "list"

编辑 - 多人评论说如果匹配,我不会返回值。如果有的话我不想退货。它可能会重复(例如:1.2. itema)。所以,我想基本上使用递归来删除它,然后返回值

4

2 回答 2

1

RemoveLeadingNums返回的条件有多种None。例如,如果if match:分支被采用。也许应该是:

if match: 
    return RemoveLeadingNums(...

None如果您有任何不是传入字符串的数据类型,您也会返回。

于 2013-01-30T16:23:25.387 回答
1

在匹配的情况下,您不会返回任何内容。它应该是:

return RemoveLeadingNums( ... )
于 2013-01-30T16:23:44.777 回答