gpt4 book ai didi

python - 根据 STR 中的条件插入 ',' - Python

转载 作者:太空宇宙 更新时间:2023-11-03 15:17:11 26 4
gpt4 key购买 nike

我正在处理一长串看起来像这样的街道名称:

1820 W 9000 SWest Jordan
455 S 500 ESalt Lake City
555 S 200 WBountiful
1000 N Green Valley PkwyHenderson
10100 W Tropicana AveLas Vegas
10305 S 1300 ESandy
10600 Southern Highlands PkwyLas Vegas
10616 S Eastern AveHenderson
111 Coors Blvd NWAlbuquerque
1170 E Gentile StLayton
1174 W 600 NSalt Lake City
1200 W Main StRiverton
....
....

我试图在城市名称之前插入一个“,”,它总是出现在一个小写字符之后,后跟一个无空格和一个大写字符。

所以这是我的想法:

我如何写出或多或少的内容:

for cities in lst:
if [char] is lower and [nextchar] is UPPER:
[insert] ',' before UPPER

最佳答案

按照@Martijn 的建议,采用组中的最后一个大写字母,也许:

import re
def fix(s):
return re.sub("([a-z]|[A-Z]+)([A-Z])",r"\1,\2", s)

给出

>>> for line in lines:
... print fix(line)
...
1820 W 9000 S,West Jordan
455 S 500 E,Salt Lake City
555 S 200 W,Bountiful
1000 N Green Valley Pkwy,Henderson
10100 W Tropicana Ave,Las Vegas
10305 S 1300 E,Sandy
10600 Southern Highlands Pkwy,Las Vegas
10616 S Eastern Ave,Henderson
111 Coors Blvd NW,Albuquerque
1170 E Gentile St,Layton
1174 W 600 N,Salt Lake City
1200 W Main St,Riverton

[免责声明:我对正则表达式很糟糕。]

关于python - 根据 STR 中的条件插入 ',' - Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20081096/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com