我有以下文本,每行有两个短语,用"\t"
分隔
RoadTunnel RouteOfTransportation
LaunchPad Infrastructure
CyclingLeague SportsLeague
Territory PopulatedPlace
CurlingLeague SportsLeague
GatedCommunity PopulatedPlace
我想得到的是在单独的单词中添加_
,结果应该是:
Road_Tunnel Route_Of_Transportation
Launch_Pad Infrastructure
Cycling_League Sports_League
Territory Populated_Place
Curling_League Sports_League
Gated_Community Populated_Place
没有像"ABTest"
或"aBTest"
这样的案例,也有像三个字在一起"RouteOfTransportation"
这样的案例,我尝试了几种方法,但都没有成功。你知道吗
我的一个尝试是:
textProcessed = re.sub(r"([A-Z][a-z]+)(?=([A-Z][a-z]+))", r"\1_", text)
但没有结果
使用正则表达式和
re.sub
。你知道吗编辑:这里还有一个,因为您的测试用例没有足够的内容来确定您到底想要什么:
根据您的需要,可以采用以下稍微不同的解决方案:
它将在紧跟在另一个字母后面的任何大写字母之前插入
_
(无论是大写还是小写)。你知道吗"TheRabbit IsBlue"
=>;"The_Rabbit Is_Blue"
"ABThing ThingAB"
=>;"A_B_Thing Thing_A_B"
它不支持特殊字符。你知道吗
组合
re.findall
和str.join
:相关问题 更多 >
编程相关推荐