How do you split a log field when its value also contains an equals sign?

partition splits once at the first occurrence and returns the prefix, the delimiter itself and the remaining text.

Python example and interpretation

The value of query=a=b remains a=b. Inspecting the middle component distinguishes an absent separator from an explicitly empty value.

['query=a=b'.partition('='),'flag'.partition('='),'flag='.partition('=')]

Result [('query', '=', 'a=b'), ('flag', '', ''), ('flag', '=', '')]

中文

日志的值中也含等号时,怎样只分出第一个键?

partition 仅以首次出现的分隔符拆分,返回前缀、分隔符本身与剩余文本三个部分。

Python 算例与适用条件

query=a=b 的值完整保留为 a=b。检查返回的中间项还能区分“没有等号”与“值为空”。

['query=a=b'.partition('='),'flag'.partition('='),'flag='.partition('=')]

结果 [('query', '=', 'a=b'), ('flag', '', ''), ('flag', '=', '')]

References / 参考来源

Keep reading / 继续阅读