2024-04-19 14:10:42 发布
网友
我正在尝试格式化此字符串,但不包括字符:()
My_name (1) Your_name (2)
期望输出:
My_name_ID_1 Your_name_ID_2
这是我的数据帧的一列。我尝试过替换,但一次只替换一个字符,以后我也想加入。你知道吗
我可以合并并替换这两个字符吗?你知道吗
您还可以使用:
s.str.replace(r"\(.*\)","").str.strip()+"_ID_"+s.str.replace(r'[^(]*\(|\)[^)]*', '')
然而,@user3483203的答案更好。:)
可以将正则表达式与str.replace一起使用:
str.replace
s.str.replace(r'(\w+)\s+\(([^\)])\)', r'\1_ID_\2')
0 My_name_ID_1 1 Your_name_ID_2 Name: 0, dtype: object
另一种选择是:
s.str.replace(r'\s+\(([^\)])\)', r'_ID_\1')
如果你不想太直白的话。你知道吗
正则表达式解释
( # matching group 1 \w+ # matches any word character ) \s+ # matches one or more spaces \( # matches the character ( ( # matching group 2 [^\)] # matches any character that IS NOT ) ) \) # matches the character )
您还可以使用:
然而,@user3483203的答案更好。:)
可以将正则表达式与
str.replace
一起使用:另一种选择是:
如果你不想太直白的话。你知道吗
正则表达式解释
相关问题 更多 >
编程相关推荐