2016-04-24 10 views
-1

Es gibt so viele Fragen über Unicode-Decodierung in SO und nichts von diesem Satz passt meine Bedürfnisse.Python Unicode-Datei reencoding

Sag mal, ich brauche einige JSON-Daten zu verarbeiten, den Teil sieht aus wie das ist:

u'message': { 
    u'body': u'\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435', 
    u'user_id': 374298, 
    u'title': u' ... ', 
    u'date': 1461496370, 
    u'read_state': 0, 
    u'id': 4325364, 
    u'out': 1 
    } 

Das einzige Ziel loszuwerden Unicode ist für Menschen lesbaren Text in „Körper“ Reihe zu bekommen:

'message': { 
    'body': 'cпасибо большое', 
    'user_id': 374298, 
    'title': ' ... ', 
    'date': 1461496370, 
    'read_state': 0, 
    'id': 4325364, 
    'out': 1 
    } 

Sobald es genügend json Daten sind, habe ich einige gefunden python-Quellcode wie folgt geändert:

#!/usr/bin/env python3 

# -*- coding: utf-8 -*- 

filePath = "/home/user/foo.txt" 

outPath = filePath + ".new" 

outText = "" 

inF = open(filePath, "rb") 
fContent = unicode(inF.read(), 'utf-8') 
inF.close() 

for everysymbol in fContent: 
    if isinstance(everysymbol, unicode): 
     outText += everysymbol.decode('cp1251') 
    else: 
     pass 

fContent = outText 


outF = open(outPath, "wb") 
outF.write(outText.decode("utf-8")) 
outF.close() 

Leider ändert dieser Code in der Zieldatei nichts. Wie dekodiere ich meine Daten wirklich, um lesbar zu sein?

+1

ändern rb und wb zu r und w – Natecat

Antwort

0
#!/usr/bin/env python3 
import json 

filePath = "/home/user/foo.txt" 

outPath = filePath + ".new" 

with open(filePath, "rb") as f: 
    fContent = json.loads(f.read()) 

with open(outPath, "wb") as f: 
    f.write(json.dumps(fContent, ensure_ascii=False).encode()) 
+1

Während dieses Code-Snippet das Problem lösen kann, [einschließlich einer Erklärung] (http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) hilft wirklich, die Qualität Ihres Posts zu verbessern. Denken Sie daran, dass Sie die Frage für Leser in der Zukunft beantworten, und diese Leute könnten die Gründe für Ihren Codevorschlag nicht kennen. – MattDMo

4

Sie Daten sind nicht JSON, aber bereits geparste JSON-Daten. Python 2 druckt Wörterbücher und Listen mit repr(), die Nicht-ASCII-Zeichen als Escape-Codes enthält. Drucken Sie die Zeichenfolge direkt seinen Inhalt zu sehen, die str() verwendet:

import json 

# Your already parsed data 
data = {u'message': { 
    u'body': u'\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435', 
    u'user_id': 374298, 
    u'title': u' ... ', 
    u'date': 1461496370, 
    u'read_state': 0, 
    u'id': 4325364, 
    u'out': 1}} 

# Write it to a file to generate real json-encoded data: 
with open('foo.json','w') as f: 
    json.dump(data,f,indent=2) 

# Display actual json-encoded file: 
with open('foo.json') as f: 
    print f.read() 
print 

# reparse the data 
with open('foo.json') as f: 
    data = json.load(f) 

# Printing dictionaries and lists escapes non-ASCII 
print data 
print 

# But print the string directly and it is fine (assuming your terminal and font supports the characters): 

print data[u'message'][u'body'] 

Ausgang:

{ 
    "message": { 
    "body": "\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435", 
    "user_id": 374298, 
    "title": " ... ", 
    "date": 1461496370, 
    "read_state": 0, 
    "id": 4325364, 
    "out": 1 
    } 
} 

{u'message': {u'body': u'\u0441\u043f\u0430\u0441\u0438\u0431\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u0435', u'user_id': 374298, u'title': u' ... ', u'date': 1461496370, u'read_state': 0, u'id': 4325364, u'out': 1}} 

спасибо большое 

Beachten Sie, dass Python 3 nicht mehr druckbar Nicht-ASCII-entkommt für repr().