Article View: pl.comp.lang.python
Article #27141Re: Unicode
From: =?UTF-8?Q?Cezary
Date: Thu, 19 Dec 2019 13:16
Date: Thu, 19 Dec 2019 13:16
78 lines
2022 bytes
2022 bytes
W dniu 18.12.2019 o 11:07, damian@swistowski.org pisze: > - print(p['content']) # w tej linii błąd, chociaż kilka wypisuje. > + print(p.get('content', '')) # w tej linii błąd, chociaż kilka wypisuje. > Ostatecznie zrobiłem w taki sposób, można ulepszyć, ale juz jest czytelnie: #!/usr/bin/python3.7 import json import datetime def marginesy(l, margines): k = "\n" + margines l = k.join(l.split("\n")) return (l) for i in range(29, 0, -1): file = 'message_' + str(i) + '.json' with open(file) as json_file: data = json.load(json_file) sender_time_old = 'dupa' for p in reversed(data['messages']): sender_name = p["sender_name"].encode("latin").decode("utf") if sender_name == 'Cezary Grądys': margines = '' else: margines = '\t' time = datetime.datetime.fromtimestamp(p["timestamp_ms"] / 1000.0).strftime("%Y-%m-%d %H:%m") sender_time = sender_name + ' ' + time if sender_time_old != sender_time: print("\n" + margines + sender_time) sender_time_old = sender_time if 'content' in p: print(margines + marginesy(p['content'].encode("latin").decode("utf"), margines)) if 'gifs' in p: print(margines, end='') print(p['gifs']) if 'photos' in p: print(margines, end='') print(p['photos']) if 'sticker' in p: print(margines, end='') print(p['sticker']) if 'videos' in p: print(margines, end='') print(p['videos']) if 'share' in p: print(margines, end='') print(p['share']) if 'reactions' in p: print(margines, end='') print(p['reactions']) -- Cezary Grądys czarekgr@wa.onet.pl
Message-ID:
<5dfb6a25$0$521$65785112@news.neostrada.pl>
Path:
polish.pugleaf.net!archive.newsdeef.eu!apf1.newsdeef.eu!not-for-mail
References:
<5df7b2b9$0$503$65785112@news.neostrada.pl> <f2b06697-c712-4ebe-a2c4-a9b7bfab249e@googlegroups.com>