Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
上下文:我想使用 Python 遍历一堆 json 文档(cosmodb)并删除键值中字符串的最后一部分
我想转这个:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"
进入这个:
"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"
任何帮助将不胜感激。
您可以使用以下代码轻松实现这一点:
result = my_string.rsplit('$', 1)[0] + "$"
其行为如下:
>>> my_string = "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02" >>> print(my_string.rsplit('_', 1)[0]) "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"