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

标签 python string split

如何在 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/

相关文章:

python - 为什么python pandas不提供linux whl文件

r - R中大写和小写字符之间的字符串拆分?

vba - 按可变行数拆分 Excel 电子表格(例如 : about 5, 000 行加上最多 1,000 行)

python - 如何将 tkinter Canvas 滚动到绝对位置?

Python异或汉明距离

Python 多处理 - 从 3 个不同的函数返回值

java - 在 String 中的特定位置查找模式并将其替换为 Java 中的其他内容

python - DataFrame 列将字典列表存储为字符串 : Parse it and build a new dataframe

python - 正则表达式或分割 (' ' )?

Python:将一个大文本文件分割为多个标题