aitech-pbr-rjawor/materiały na PPB (wykład)/json_script.ipynb

8.9 KiB
Raw Blame History

#procedura napisywania plików ipynb (generowanie nagłówka i metadanych)
import json

def modjup(filen,numer,tytul,typ,author,email,lang,title,year):
    zerocell=['![Logo 1](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech1.jpg)\n',
              '<div class="alert alert-block alert-info">\n', 
              '<h1> %s </h1>\n'%(title),
              '<h2> %s. <i>%s</i>  [%s]</h2> \n'%(numer,tytul,typ),
              '<h3> %s (%s)</h3>\n'%(author,year),
              '</div>\n',
              '\n',
              '![Logo 2](https://git.wmi.amu.edu.pl/AITech/Szablon/raw/branch/master/Logotyp_AITech2.jpg)']
    zerodict={'cell_type': 'markdown','metadata': {'collapsed': False},'source': zerocell}
    with open(filen, 'r+') as f:
        ll=json.load(f)
        ll["metadata"]["author"]=author
        ll["metadata"]["email"]=email
        ll["metadata"]["lang"]=lang
        subtitle="%s.%s[%s]"%(numer,tytul,typ)
        ll["metadata"]["subtitle"]=subtitle
        ll["metadata"]["title"]=title
        ll["metadata"]["year"]=year
    
        if not(ll['cells'][0]['source'][0]==zerocell[0]):
            ll['cells'].insert(0,zerodict)
        else:
            ll['cells'][0]=zerodict
        f.seek(0)
        json.dump(ll,f,indent=4)
#zmodyfikuj te dane
filen="01_praca_zespolowa.ipynb"

numer="1"
tytul=""
typ="wyklad"

author="Krzysztof Jassem"
email="jassem@amu.edu.pl"
lang= "pl"
title=""
year="2021"

#uruchom procedurę
modjup(filen,numer,tytul,typ,author,email,lang,title,year)
---------------------------------------------------------------------------
UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-7-dc51dba53985> in <module>
     13 
     14 #uruchom procedurę
---> 15 modjup(filen,numer,tytul,typ,author,email,lang,title,year)

<ipython-input-3-2ca9011d1935> in modjup(filen, numer, tytul, typ, author, email, lang, title, year)
     13     zerodict={'cell_type': 'markdown','metadata': {'collapsed': False},'source': zerocell}
     14     with open(filen, 'r+') as f:
---> 15         ll=json.load(f)
     16         ll["metadata"]["author"]=author
     17         ll["metadata"]["email"]=email

~\anaconda3\lib\json\__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    291     kwarg; otherwise ``JSONDecoder`` is used.
    292     """
--> 293     return loads(fp.read(),
    294         cls=cls, object_hook=object_hook,
    295         parse_float=parse_float, parse_int=parse_int,

~\anaconda3\lib\encodings\cp1250.py in decode(self, input, final)
     21 class IncrementalDecoder(codecs.IncrementalDecoder):
     22     def decode(self, input, final=False):
---> 23         return codecs.charmap_decode(input,self.errors,decoding_table)[0]
     24 
     25 class StreamWriter(Codec,codecs.StreamWriter):

UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 8350: character maps to <undefined>