2017-10-12 1 views
0

"title":, "date": 등으로 구분되는 문장이 있습니다.특정 정규 표현식을 기반으로 문자열을 분할하는 방법은 무엇입니까? (파이썬에서)

이 구분 기호를 기준으로이 문자열을 분할하고 싶습니다. . 지금은이가 ..

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

그것은 JSON이 될 수 있습니다처럼, 당신은()'를 구문 분석'json.loads을 사용할 수 있습니다 소리. – Barmar

+0

나는 이것을 사용했지만, 어떤 이유로 든 valueerrors를 얻고있었습니다. 나는 이것이 더 쉬울 것이라고 생각한다. –

+1

입력의 전체 샘플을 보여준다. – Barmar

답변

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 '} 
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']) 

DEMO

관련 문제