How could I split a string by comma which contains commas itself in Python? Let's say the string is:
object = """{"alert", "Sorry, you are not allowed to do that now, try later", "success", "Welcome, user"}"""
How do I make sure I only get four elements after splitting?
>>> 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+ you can simply use literal_eval(obj)
.
>>> import re
>>> re.findall(r'\"(.+?)\"', obj)
['alert', 'Sorry, you are not allowed to do that now, try later',
'success', 'Welcome, user']
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.