-2

上下文:我想使用 Python 遍历一堆 json 文档(cosmodb)并删除键值中字符串的最后一部分

我想转这个:

"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$1234$uspc02"

进入这个:

"id": "randomstuff-channelruleprintermap-1234-$pc2$randomstuff$"

任何帮助将不胜感激。

4

1 回答 1

0

您可以使用以下代码轻松实现这一点:

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$"
于 2020-05-05T05:54:20.650 回答