我有一个字符串是一堆句子,被“标题”:,“日期”:等等分开。
我想根据这些分隔符拆分此字符串。现在我有这个..
line = re.split(r'("[a-z]: ")', line)
{"date": "Jul 18, 2017, 4:10 AM",
"text": "Best / cheapest",
"state_or_country_or_utility": "Norway Travel Forum",
"responses": ["The local train www.nsb.no"],
"title": "airport transfer "}
答案 0 :(得分:0)
您可以使用ast.literal_eval
:
import ast
s = '{"date": "Jul 18, 2017, 4:10 AM", "text": "Best / cheapest", "state_or_country_or_utility": "Norway Travel Forum", "responses": ["The local train www.nsb.no"], "title": "airport transfer "}'
final_data = ast.literal_eval(s)
输出:
{'date': 'Jul 18, 2017, 4:10 AM', 'text': 'Best / cheapest', 'state_or_country_or_utility': 'Norway Travel Forum', 'responses': ['The local train www.nsb.no'], 'title': 'airport transfer '}
答案 1 :(得分:0)
您的输入是JSON,您可以使用json.loads()
。
导入json
data = '''{"date": "Jul 18, 2017, 4:10 AM",
"text": "Best / cheapest",
"state_or_country_or_utility": "Norway Travel Forum",
"responses": ["The local train www.nsb.no"],
"title": "airport transfer "}'''
result = json.loads(data)
print('Title = ', result['title'])
print('Date = ', result['date'])