Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss
Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Unicode

24 views
Skip to first unread message

Cezary Grądys

unread,
Dec 16, 2019, 11:37:14 AM12/16/19
to

Witam.
Sorry za lamerstwo, ale mam w pliku (json) linie typu:

"sender_name": "Cezary Gr\u00c4\u0085dys",

Chcę z tego zrobić

"sender_name": "Cezary Grądys",


Szkielet skryptu taki:


#!/usr/bin/python3

import sys
import re

try:
f = open(sys.argv[1]) if len(sys.argv) > 1 else sys.stdin
except FileNotFoundError:
print("Brak pliku")
exit(1)
except PermissionError:
print("Brak dostępu do pliku")
exit(1)



for line in f:
line = line.strip()

# co tu wstawić ????

print(line)



--
Cezary Grądys
czar...@wa.onet.pl

zzz

unread,
Dec 16, 2019, 8:20:02 PM12/16/19
to
Cezary Grądys napisal(a):
>
> Witam.
> Sorry za lamerstwo, ale mam w pliku (json) linie typu:
>
> "sender_name": "Cezary Gr\u00c4\u0085dys",
>
> Chcę z tego zrobić
>
> "sender_name": "Cezary Grądys",
>

a = "Cezary Gr\u00c4\u0085dys"
a.encode(latin2).decode(utf)

albo siekierką:
a.replace("\u00c4\u0085","ą")

a poprawnie:
otworzyć plik podając jego kodowanie
najlepiej za pomocą json-a(import json)

--
=============== zZzZz =============================

Cezary Grądys

unread,
Dec 17, 2019, 1:57:41 PM12/17/19
to
W dniu 17.12.2019 o 02:15, zzz pisze:
> Cezary Grądys napisal(a):
>>
>> Witam.
>> Sorry za lamerstwo, ale mam w pliku (json) linie typu:
>>
>> "sender_name": "Cezary Gr\u00c4\u0085dys",
>>
>> Chcę z tego zrobić
>>
>> "sender_name": "Cezary Grądys",
>>
>
> a = "Cezary Gr\u00c4\u0085dys"
> a.encode(latin2).decode(utf)
>
> albo siekierką:
> a.replace("\u00c4\u0085","ą")
>
> a poprawnie:
> otworzyć plik podając jego kodowanie
> najlepiej za pomocą json-a(import json)
>


Dzięki za odpowiedź.

Jsonem się zainteresowałem, na razie mam tak:


import json
with open('message_1.json') as json_file:
data = json.load(json_file)
for p in data['messages']:

print(p["sender_name"])
print(p['timestamp_ms'])
print(p['content']) # w tej linii błąd, chociaż kilka wypisuje.



No i mam błąd, wydaje się, że związany z wielkoscią pliku, jak skróciłem
znacznie działa, komunikaty między liniami wypisanymi przez print:


Traceback (most recent call last):
print(p['content'])
KeyError: 'content'

Process finished with exit code 1

Myślałem o użyciu gron + sed itp i pewnie na tym się skończy, ale
wolał bym bardziej elegancko i przy okazji powalczyć z wtórnym
analfabetyzmem.


--
Cezary Grądys
czar...@wa.onet.pl

dam...@swistowski.org

unread,
Dec 18, 2019, 5:07:29 AM12/18/19
to
- print(p['content']) # w tej linii błąd, chociaż kilka wypisuje.
+ print(p.get('content', '')) # w tej linii błąd, chociaż kilka wypisuje.

Cezary Grądys

unread,
Dec 19, 2019, 7:16:51 AM12/19/19
to
W dniu 18.12.2019 o 11:07, dam...@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
czar...@wa.onet.pl
0 new messages