🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: pl.comp.lang.python
5 messages
5 total messages Started by Cezary Grądys Mon, 16 Dec 2019 16:37
Unicode
#28661
Author: Cezary Grądys
Date: Mon, 16 Dec 2019 16:37
42 lines
1532 bytes
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
czarekgr@wa.onet.pl
Re: Unicode
#28662
Author: zzz
Date: Tue, 17 Dec 2019 01:15
24 lines
1373 bytes
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 =============================
Re: Unicode
#28663
Author: Cezary Grądys
Date: Tue, 17 Dec 2019 18:57
59 lines
2374 bytes
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
czarekgr@wa.onet.pl
Re: Unicode
#28664
Author: damian
Date: Wed, 18 Dec 2019 10:07
43 lines
2154 bytes
W dniu poniedziałek, 16 grudnia 2019 16:37:14 UTC użytkownik Cezary Grądys napisał:
> 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)
> 
> 
> 


-  print(p['content']) # w tej linii błąd, chociaż kilka wypisuje.
+ print(p.get('content', '')) # w tej linii błąd, chociaż kilka wypisuje.
Re: Unicode
#28665
Author: Cezary Grądys
Date: Thu, 19 Dec 2019 12:16
79 lines
3177 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
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads