gpt4 book ai didi

python - 用于拆分包含逗号的字符串的正则表达式?

转载 作者:太空狗 更新时间:2023-10-30 01:01:50 27 4
gpt4 key购买 nike

如何在 Python 中用逗号拆分包含逗号本身的字符串?假设字符串是:

object = """{"alert", "Sorry, you are not allowed to do that now, try later", "success", "Welcome, user"}"""

如何确保拆分后只得到四个元素?

最佳答案

>>> from ast import literal_eval
>>> obj = '{"alert", "Sorry, you are not allowed to do that now, try later", "success", "Welcome, user"}'
>>> literal_eval(obj[1:-1])
('alert', 'Sorry, you are not allowed to do that now, try later', 'success', 'Welcome, user')

On Python3.2+您可以简单地使用 literal_eval(obj)

关于python - 用于拆分包含逗号的字符串的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090509/

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