2013-05-01 2 views
0

나는 다음과 같은 문자열, 나는 다음과 같은 작업을하고있는 중이 야문제는 JSON 파이썬 딕셔너리에 문자열을 전달

"""{"created_at":"Mon Mar 11 20:15:36 +0000 2013","id":311208808837951488,"id_str":"311208808837951488","text":"ALIENS ENTRATE E' IMPORTANTE!!! \n\n\n\nMTV's Musical March Madness ritorna il 18 marzo...Siete pronti A http:\/\/t.co\/ABXEfquTJw via @Hopee_dream","source":"\u003ca href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\"\u003eTwitter for Android\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1025970793,"id_str":"1025970793","name":"Tom's Perfection\u2665","screen_name":"_MyGreenEyes_","location":"","url":null,"description":"Angel,don't you cry,i'll meet you on the other side.\u2661","protected":false,"followers_count":387,"friends_count":520,"listed_count":1,"created_at":"Fri Dec 21 08:39:17 +0000 2012","favourites_count":174,"utc_offset":null,"time_zone":null,"geo_enabled":true,"verified":false,"statuses_count":772,"lang":"it","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/a0.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/si0.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/a0.twimg.com\/profile_images\/3363059730\/3d791e51eefa800150cd99917abc1d2c_normal.jpeg","profile_image_url_https":"https:\/\/si0.twimg.com\/profile_images\/3363059730\/3d791e51eefa800150cd99917abc1d2c_normal.jpeg","profile_banner_url":"https:\/\/si0.twimg.com\/profile_banners\/1025970793\/1362500832","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"entities":{"hashtags":[],"urls":[{"url":"http:\/\/t.co\/ABXEfquTJw","expanded_url":"http:\/\/tl.gd\/l9f5j7","display_url":"tl.gd\/l9f5j7","indices":[101,123]}],"user_mentions":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"medium"}""" 

API를 스트리밍 트위터에서 수집 한

대신

개조 문자가 파이썬 사전에이 문자열을 구문 분석하는 데 문제가 있음을 나에게 알립니다.

하지만 json_string.strip() 작업을하고 있습니다. 내 문자열에서 그런 것들을 제거 할 줄 알았는데 ..

내가 뭘 잘못하고 있니?

+0

: 실수로 내가 DICT에서 값을 끌어 사용 된 키에 # 기호를 앞에 추가했기 때문에 내 경우

는이 문제였다 동작을 Python 2.7 또는 3.3과 함께 사용합니다. 'json_string = "" "..." ""을 삼중 따옴표로 묶어 쓰면 JSON 문자열에 리터럴 새줄 문자를 넣는 것은 불법이므로'loads'가 실패합니다. 그러나''json_string = r "" "..." ""'이라고 쓰면 제대로 작동하고 원하는 결과물을 출력 할 수 있습니다. 물론 중간에 줄 바꿈을 인쇄하지만 줄을 잘리지는 않습니다. 나는 2.7과 3.3 모두에서 내가 생각할 수있는 모든 다른 변이를 시도했고, 그들 중 누구도 당신이 묘사하는 문제를 가지고 있지 않다. 실제 코드를 보여줄 수 있습니까? – abarnert

답변

2

str.strip() 메서드는 문자열의 시작과 끝 부분의 공백 문자 만 제거합니다. 중간 어디에도 없습니다.

문자열의 모든 줄 바꿈을 제거하려면, 당신은 할 수 :

"some\n\n\nstring".replace("\n", "") 

또는

"some\n\n\nstring".translate(None, "\n") 

첫 번째

아마 읽고 이해하기 쉽다.

+0

아직 손에 문제가 해결되지 않았습니까 ?? 여러 개의 "\ n \ n \ n"은 함께 클럽 활동을합니까? – Fraz

+0

@Fraz 테스트 해 보면 효과가 있습니다. 함께 무리가 있어도 이러한 인스턴스는 모두 제거됩니다. – Anorov

1

문자가 \n 기호 다음에 잘리는 유사한 문제가 발생했습니다. 나는이 문제를 재현 할 수있는 방법을 찾을 수 없습니다

print jsn_dict['#text'] 
관련 문제