I am working on tag recommendation and need to convert all words in quotes to a single phrase. For example
我正在处理标签推荐,需要将引号中的所有单词转换为单个短语。例如
'In Absence of Good Men' to InAbsenceOfGoodMen
1 个解决方案
#1
1
Here's one way, using re.sub
and a callback:
这是一种方法,使用re.sub和回调:
>>> import re
>>> string = "'In Absence of Good Men'"
>>> re.sub("'(.*?)'", lambda x: x.group(1).replace(' ', ''), string)
'InAbsenceofGoodMen'
You can make this a bit more readable, by moving the lambda out into its own function:
通过将lambda移动到它自己的函数中,你可以使它更具可读性:
def foo(m):
if m:
return m.group(1).replace(' ', '')
string = "'In Absence of Good Men'"
new_string = re.sub("'(.*?)'", foo, string)
You can catch non-matches and handle errors better this way.
您可以通过这种方式捕获不匹配并处理错误。
#1
1
Here's one way, using re.sub
and a callback:
这是一种方法,使用re.sub和回调:
>>> import re
>>> string = "'In Absence of Good Men'"
>>> re.sub("'(.*?)'", lambda x: x.group(1).replace(' ', ''), string)
'InAbsenceofGoodMen'
You can make this a bit more readable, by moving the lambda out into its own function:
通过将lambda移动到它自己的函数中,你可以使它更具可读性:
def foo(m):
if m:
return m.group(1).replace(' ', '')
string = "'In Absence of Good Men'"
new_string = re.sub("'(.*?)'", foo, string)
You can catch non-matches and handle errors better this way.
您可以通过这种方式捕获不匹配并处理错误。