Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何根据某些规则更改字符串?_Python_String - Fatal编程技术网

Python 如何根据某些规则更改字符串?

Python 如何根据某些规则更改字符串?,python,string,Python,String,我有以下文本,每行有两个短语,用“\t” 我想得到的是将\uuu添加到单独的单词中,结果应该是: Road_Tunnel Route_Of_Transportation Launch_Pad Infrastructure Cycling_League Sports_League Territory Populated_Place Curling_League Sports_League Gated_Community Populated_Place 没有像“ABTest”或“A

我有以下文本,每行有两个短语,用
“\t”

我想得到的是将
\uuu
添加到单独的单词中,结果应该是:

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

>>> import re
>>> s = '''LaunchPad   Infrastructure
... CyclingLeague   SportsLeague
... Territory   PopulatedPlace
... CurlingLeague   SportsLeague
... GatedCommunity  PopulatedPlace'''
>>> subbed = re.sub('([A-Z][a-z]+)([A-Z])', r'\1_\2', s)
>>> print(subbed)
Launch_Pad   Infrastructure
Cycling_League   Sports_League
Territory   Populated_Place
Curling_League   Sports_League
Gated_Community  Populated_Place
编辑:这里还有一个,因为您的测试用例没有涵盖足够的内容来确定您到底想要什么:

>>> re.sub('([a-zA-Z])([A-Z])([a-z])', r'\1_\2\3', 'ABThingThing')
'AB_Thing_Thing'

结合
re.findall
str.join

>>> "_".join(re.findall(r"[A-Z]{1}[^A-Z]*", text))

根据您的需要,可以使用稍微不同的解决方案:

import re
result = re.sub(r"([a-zA-Z])(?=[A-Z])", r"\1_", s)
它将在另一个字母后面的任何大写字母(无论是大写还是小写)之前插入

  • “兔子是蓝色的”
    =>
    “兔子是蓝色的”
  • “ABThing ThingAB”
    =>
    “A\u B\u A\u B”

它不支持特殊字符。

看起来您希望我们为您编写一些代码。虽然许多用户愿意为陷入困境的程序员编写代码,但他们通常只在海报已经试图自己解决问题时才提供帮助。演示这项工作的一个好方法是包括您迄今为止编写的代码、示例输入(如果有)、预期输出和实际获得的输出(控制台输出、回溯等)。你提供的细节越多,你可能得到的答案就越多。检查和。顺便说一句。我认为额外的测试用例会很好。
HTTPResponse
CSV
应该转换成什么?谢谢你的建议,@Rogalski,我应该把我的代码放到我的问题中。下次我会注意的。我会用
([A-zA-Z])
替换
([A-Z][A-Z]+)
,这样像
“BThing”
这样的模式就可以工作了。注意,它不支持像
“ABThing”
@QuentinRoy这样的模式是的,我们目前甚至不知道“BThing”的预期行为是什么。OP考虑“BT”一词吗?无论如何,谢谢你的评论,基于OP的实际规格,他现在可以选择正确的正则表达式,或者自己调整它。@timgeb,很抱歉模棱两可,我已经修改了我的问题
import re
result = re.sub(r"([a-zA-Z])(?=[A-Z])", r"\1_", s)