2016-08-21 4 views
0

json 파일을로드하고 나중에 구문 분석하려고합니다. 그러나 출력에서 ​​'u'문자가 계속 표시됩니다. 문제를 해결하는 인코딩 = 'utf-8'로 파일을 열려고했습니다. 파이썬 2.7을 사용하고 있습니다. 출력에서 'u'문자를 무시하고 타는 직접적인 접근법이나 해결 방법이 있습니까?파이썬 출력에서 ​​유니 코드 문자를 제거하는 방법은 무엇입니까?

import json 
import io 


with io.open('/tmp/install-report.json', encoding='utf-8') as json_data: 
    d = json.load(json_data) 
    print d 

O/P

{u'install': {u'Status': u'In Progress...', u'StartedAt': 1471772544,}} 

추신 :이 게시물 Suppress the u'prefix indicating unicode' in python strings 물마루 갔다하지만 사용 json.dumps 파이썬 2.7을위한 솔루션

+1

: [** 없음 **] (http://stackoverflow.com/questions/761361/suppress-the-uprefix-indicating-unicode-in-python-strings). – Jan

+0

왜 문제입니까? –

+0

유니 코드는이 문제와 아무 관련이 없습니다. 이것은 문자열로 가득 찬 사전의 파이썬 ('repr()') 표현입니다. JSON과 같은 다른 형식의 표현을 원할 경우 해당 형식에 대한 인코더를 사용하십시오. – bobince

답변

2

을 가지고 변환을 디코딩하지 않습니다 문자열로 변환

data = json.dumps(d, ensure_ascii=False).decode('utf8') 
print data 
+0

@ cool77 문제를 해결했다면 답을 올바른 것으로 표시하십시오 – Naruto

+1

해결책을 시도했습니다. 도움이됩니다. 하지만 나중에 스크립트에서 json.load에서 'd'라는 사전을 사용해야합니다. json.dumps를 사용하여, 나는 나중에 사전 에서처럼 반복 할 수없는 문자열을 얻습니다./ – cool77

+0

그래서'd' 사전과'data' 문자열을 모두 유지하고 필요한 것을 사용하십시오. – bobince

0

u 그냥 indicat 유니 코드 문자열 당신이 문자열을 인쇄 할 경우에는 표시되지 않습니다 :

d = {u'install': {u'Status': u'In Progress...', u'StartedAt': 1471772544}} 

print 'Status:',d[u'install'][u'Status'] 

출력 : 한마디로

Status: In Progress... 
관련 문제