-
Notifications
You must be signed in to change notification settings - Fork 0
/
PYTHON.txt
4769 lines (4205 loc) · 246 KB
/
PYTHON.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
https://github.com/gto76/python-cheatsheet
#> MODULE, GENERAL
#> VIRTUALENV
#> PIP
#> PYINSTALLER, INNO SETUP COMPILER - generate python programs to executables
#> SHORTCUTS VSCODE
#> SHORTCUTS INTELLIJ IDEA IDE, CHROME, WINDOWS
#> OPERATORS, INPUTS, OUTPUTS, ARGUMENTS, STATISTICS
#> RANDOM, SECRETS
#> STRING
#> REGEX
#> LIST
#> TUPLE
#> DICTIONARY
#> SETS
#> CONTROL STRUCTURES and ITERATIONS
#> FUNCTIONS, DECORATORS
#> GENERATORS, YIELD
#> LAMBDA, MAP, FILTER
#> CLASSES, DATACLASSES
#> TXT FILES
#> JSON FORMAT
#> XML FORMAT
#> URLLIB
#> MODULE - CSV
#> MODULE - ZIPFILE - handling ZIP files
#> MODULE - GSPREAD - API for Google Sheets
#> MODULE - OPENPYXL - working with excel worksheets
#> MODULE - XLWINGS - working on the fly with open excel worksheets
#> MODULE - PYTHON-DOCX - working with word docx
#> MODULE - WIN32COM - creating worksheets as pdf from xlsx
#> MODULE - PyPDF2 - working with pdfs
#> MODULE - PYODBC - odbc-access (MS Access)
#> MYSQL - MARIADB - HEIDISQL
#> MYSQL - MARIADB - SQL
#> MYSQL - MYSQL.CONNECTOR
#> SQL ALCHEMY
#> SQLITE3 SQL
#> MODULES - DATES, DATETIME, CALENDAR, TIMEIT, TIME, SYS, CTYPES
#> MODULE - CURRENCYCONVERTER - currency conversion
#> MODULE - PYCOUNTRY - Countries, Currency, Language
#> MODULES - SMTPLIB, MIMEText - sending emails
#> MODULE - PATHLIB - interacting with the operating system
#> MODULE - OS, SHUTIL, FILECMP, DIRCMP, Send2Trash - interacting with the operating system
#> MODULE - LOGGING
#> MODULE - UNITTEST
#> MODULE - DOTENV - hide environment informations
#> MODULE - COLLECTIONS - counter, defaultdic
#> MODULE - ITERTOOLS - products, combinations
#> MODULE - NUMPY - basis for different other modules
#> MODULE - PANDAS - analyzing and working with data
#> MODULE - MATPLOTLIB - working with charts
#> MODULE - SELENIUM - browser automatization
#> MODULE - BEAUTIFUL SOAP - webscraping
#> MODULE - REQUESTS - workings with APIs
#> MODULE - SCRAPY - webscraping using spiders
#> MODULE - FTPLIB - ftp data transfer
#> MODULE - PYQT - making GUIs
#> MODULE - TKINTER - making GUIs
#> MOUULE - KIVY - making GUIs
#> MODULE - PYGAME - making games
#> MODULE - PYTHONCOM - make new formula in excel with python-function
#> MODULE - FLASK, ZAPPA, AWS - making an API
#> MODULE - FLASK, HEROKU, GUNICORN - making an API
#> MODULE - FASTPI, UVICORN - making an API
#> MODULE - ICECREAM - print for debugging
#> MODULE - TA-LIB - technical analysis of financial market
#> MODULE - YFINANCE - date from Yahoo Finance
#> MODULE - PLOTLY - data visualization
#> MODULE - DJANGO - framework for webdevelopment
#> DOLT - Databases like Repos
#> PYTHONANYWHERE - run python scripts in the cloud
#> HEROKU - run python scripts in the cloud
###### MODULE, GENERAL
import math => Import python module
from math import ceil => Import specific function of a module
import numpy as np => Import a module with abbreviation
if __name__ == '__main__': => Program execution starts from here
python --version => Check which python version is installed
=> Use modules / functions from other folder
=> (not working with eg. yahoocrawler.py and rapidtechtools.py - all modules, wether they are needed or not have to imported in the program...)
""
import sys, os
# Create Path to module
sys.path.append(os.path.join('C:/', 'Users\path_to_module'))
# Import Module RapidTechTools
import RapidTechTools as rtt
""
###### VIRTUALENV
pip install virtualenv => Install virtualenv
pip list => Global packages installed
where python => Shows where the python-file is (Windows)
which python => Shows where the python-file is (Linux)
py -3.12 -m venv openai => Create a virtual environment with a specific installed python version
python -m virtualenv env => Create new virtual environment env
python3 -m venv env => Create new virtual environment env (for python3 eg. on MacOS)
pip freeze --local > requirements.txt => Extracts all the modules / dependencies to a txt-file
deactivate => Go back to the global environment
virtualenv -p C:\..path to..\Python37\python.exe py37_env => Create new virtual env with specific python-version
eg. virtualenv -p C:\\Users\\WRSPOL\\AppData\\Local\\Programs\\Python\\Python312\\python.exe selenium
py37_env\Scripts\activate => Activate new virtual env named py37_env
python --version => Shows the used python version in the virtual env
pip install -r requirements.txt => Install all the packages from requirements.txt
py -0p => Windows: Show all installed python-versions + paths
=> Activate environment
""
venv\Scripts\activate
when activated: pip list # Now only shows the installed modules for the virtual environment
pip install package # Only installs in the activated virtual environment
""
=> install virtual environment with specific python-version
""
download and install the python-version - Important: remember the path to the python.exe
create a venv with: virtualenv envName -p path\to\new_python.exe
""
###### PIP
pip install moduleXY => install module
pip install moduleXY --upgrade => upgrade module
pip install -Iv pyinstaller==4.8 => Install specific version
pip uninstall module XY => uninstall module XY
pip list => show installed modules in actual environment
pip freeze > requirements.txt => output modules in requirements.txt
pip install -r requirements.txt => install modules from requirements.txt
###### PYINSTALLER, INNO SETUP COMPILER - generate python programs to executables
pip install pyinstaller => Installation
pip install -Iv pyinstaller==4.8 => Install specific version
pyinstaller prg.py => Generate the bundle in a subdirectory called dist.
pyinstaller --onefile prg.py => Generate only one file
python -m Pyinstaller --onefile prg.py => Generate only one file (to get for sure the pyinstaller from the actual env)
pip install -U pyinstaller-hooks-contrib => In case of problems when creating the exe (eg. some problems with pydantic)
--collect-data moduleXY => Collect data for specific python module
--copy-metadata pandas_ta => add this when some modules are not found
--hidden-import=pymssql => When some modules are making problems - sometimes this helps
pyinstaller --onefile --collect-datas=trafilatura xyz.py
pyinstaller --onefilFe --collect-data fake_useragent checkMapsSel2.py
pyinstaller --onefile --collect-data frozendict stockma3.py => <frozen importlib._bootstrap> error when using yfinance
pyinstaller --onefile --collect-data langchain --copy-metadata=tqdm --copy-metadata=regex --copy-metadata requests --copy-metadata packaging --copy-metadata filelock URLanswering.py
pyinstaller --onefile --hidden-import=tiktoken_ext.openai_public --hidden-import=tiktoken_ext checkRSS.py
--exclude-module matplotlib => when there is a depreciating warning from matlib
--icon=app.ico => Generate file with icon
--hidden-import=pkg_resources.py2_warn => Generating under mac os sometimes only work with this params
--hidden-import=cmath => Generating under mac os sometimes only work with this params
pyinstaller --onefile main.spec
prg.exe => Open this file to start the python-program
yfinance module => only working with import yfinance (not with import yfinance as yf)
=> Problems with some added python modules (eg. pycountry)
""
https://groups.google.com/g/pyinstaller/c/OYhJdeZ9010/m/32g3-T8XBAAJ
Create hook-file hook-pycountry.py with content:
from PyInstaller.utils.hooks import copy_metadata, collect_data_files
datas = copy_metadata("pycountry") + collect_data_files("pycountry")
Compile Program with
pyinstaller --onefile --exclude-module matplotlib --additional-hooks-dir=. TestPyCountry.py
""
=> Problems with path when executing from py and exe
""
config_name = 'creds.json' # Define the config file name
""
=> Determine if application is a script file or frozen exe
""
if getattr(sys, 'frozen', False): # Get path when starting as executable
application_path = os.path.dirname(sys.executable)
elif __file__: # Get path when running from IDE as py-file
application_path = os.path.dirname(__file__)
config_path = os.path.join(application_path, config_name) # Final Config Path
""
=> Using Inno Setup Compiler for creating a setup.exe for all files
""
https://jrsoftware.org/isdl.php
- Application Information => provide application informations (appName, appVersion, publisher, website)
- Application Files => select application main executable file and add all necessary folder and files
- Application File Association => no selection
- Application Documentation => select files which should be shown before / after the installation
- Setup Languages => select which languages should be included during installation
- Save installation script
- Final setup.exe is stored in the "Output"-folder
""
=> pathlib error when running executable on Mac
""
delete all created folder from pyinstaller an try again to create the excecutable
""
###### SHORTCUTS VSCODE
Ctrl K Ctrl C => Comment / Uncomment selected lines ("/" on numblock) (commenting in VS Code)
Ctrl K Ctrl U => Comment / Uncomment selected lines ("/" on numblock) (commenting in VS Code)
Ctrl F => Find in File
Ctrl H => Replace in File
Ctrl L => Selects whole line
Ctrl Shift F => Search in all files
Ctrl Shift H => Replace in Path (in all Files)
Ctrl 1/2/3 => Jump to this Grouping Window
F3 => Find next
Ctrl G => Goto line
Shift Alt Up/Down => Copy / Duplicate lines
Shift Alt Left/Rigth => Expand / Shrink selection
Alt Up/Down => Move line
Alt Enter => Suggestions for fix error, function informations
Alt 1 => Open the project windows (on the left side)
Alt F7 => Show usage of the variable, function, class
Esc => Focus back on the editor window
Ctrl K Ctrl 0 => Collapse everything
Ctrl K Ctrl J => Expands everything
Ctrl K Ctrl L => Collapse block ("-" on numblock)
Ctrl K Ctrl L => Expand block ("+" on numblock)
Ctrl A => Select whole file
Ctrl (Shift) Z => Undo (backwards) / Redo (forwards)
Ctrl Shift Left => Select word to the beginning
Ctrl Shift Right => Select word to the end
Ctrl Left / Right => Go one word left
Ctrl Shift K => Delete current line
Ctrl Del => Delete to end of word
Ctrl Backspace => Delete to beginning of word
Ctrl B => Hide/Show sidebar
Ctrl J => Hide/Show terminal
Ctrl P => Show command line (eg. open a file where the name is known)
Ctrl Shift P => Open the the command panel
Ctrl TAB => Toggle through the open files in the editor
Ctrl W => Close File
Ctrl Alt Up/Down => MultiCursor - Select more cursors in rows for multiple change in rows
Ctrl D => MultiCursor - Select Cursor at multiple positions for the same name
Ctrl Shift L => Select all occurences of this word in the document and change all with one change
Ctrl D => Select whole word
Ctrl Click => Jump to the function definition
Ctrl W => Close Tab
Ctrl Space => Show suggestions for the actual code function / method
###### SHORTCUTS INTELLIJ IDEA IDE, CHROME, WINDOWS
=> https://www.shortcutfoo.com/app/dojos/intellij-idea-win/cheatsheet
""
Ctrl / # Comment / Uncomment selected lines ("/" on numblock) (commenting in VS Code)
Ctrl / # Comment / Uncomment selected lines ("/" on numblock) (commenting in VS Code)
Ctrl Alt F7 # Find usages
Ctrl F # Find in File
Ctrl R # Replace in File
Ctrl Shift R # Replace in Path (in all Files)
F2 # jump to the next error
F3 # Find next
Ctrl G # Goto line
Ctrl D # Copy / Duplicate lines
Ctrl X # Cut line (when nothing selected)
Ctrl C # Copy line (when nothing selected)
Alt Shift Down/Up # Move line
Ctrl E # Recent opened files
Ctrl TAB # Switch windows in IDE
Ctrl (Shift) W # Select / deselect parts of the code step by step
Alt Enter # Suggestions for fix error, function informations
Alt 1 # Open the project windows (on the left side)
Alt F7 # Show usage of the variable, function, class
Esc # Focus back on the editor window
Ctrl Shift - # Make screen smaller
Ctrl Shift + # Make screen bigger
Ctrl - # Collapse block ("-" on numblock)
Ctrl + # Expand block ("+" on numblock)
Ctrl A # Select whole file
Ctrl (Shift) Z # Undo (backwards) / Redo (forwards)
Ctrl Shift Left # Select word to the beginning
Ctrl Shift Right # Select word to the end
Ctrl Left / Right # Go one word left
Ctrl Y # Delete current line
Ctrl Del # Delete to end of word
Ctrl Backspace # Delete to beginning of word
lorem + TAB # Create lorem text for html-code
p>lorem + TAB # Create lorem text inside <p>-tags
""
=> Shortcuts Chrome
=> https://www.makeuseof.com/tag/google-chrome-shortcuts-pdf/
""
F5 # Refresh site
Ctrl Tab # Go tab right
Ctrl Shift Tab # Go tab left
Ctrl F5 # Clear Cache and reload page
""
=> Shortcuts Win10
=> https://fossbytes.com/windows-keyboard-shortcuts-cheat-sheet-for-windows-10/
""
Alt Tab # Focus on other window
Ctrl C # Copy
Ctrl X # Cut
Ctrl V # Paste
""
=> Enable pushing with ssh-key in Idea and VSCode
""
Enable open ssh agent: https://dev.to/aka_anoop/how-to-enable-openssh-agent-to-access-your-github-repositories-on-windows-powershell-1ab8
Run commands: https://stackoverflow.com/questions/56490194/vs-code-bitbucket-ssh-permission-denied-publickey
""
###### OPERATORS, INPUTS, OUTPUTS, ARGUMENTS, STATISTICS
import math => Import module for math calculations
import sys => Import module for reading arguments from the command line
import statistics as stat => Using some statistics functions
5 // 2 => Result without Decimals (=> 2)
7 % 2 => Modulo / Reamainder of the division (=> 1)
d,e,f = 4,5,6 => Assigment of several varaibles
a,b = b,a => Change / swap 2 values
a += 1 => Increase value by one
i = 1_000_000 => For better readability - "_" are possible using long num-values (will be ignored)
round (77.2321, 2) => Value is rounded to two decimal places => 77.23
int(v - v%10) => Round to tens place
str(5), float("5"), int("5") => Conversion to String / Float / Int
abs(-2) => Outputs absolute value => 2
input("ENTER to continue") => Wait for pressing ENTER (or a keyboard stroke) to follow along
int(input("Age?":)) => Input age and change to int
type(var) => Returns the type of a variable
dir(var) => Shows all available methods and attributes for the object as list
var.__dict__["_rawData"] => show all attributes with the value
isinstance(x,float) => Check if x has type format float (others: str,int,list,tuple,range,dict,set,bool)
x = float("inf") => Set var to max-value (float infinite)
math.sqrt(9) => Calculates the sqrt of the value => i3
if "myVar" in locals(): => Check if a variable exists
min = a if a < b else B => Ternary Operator for if statement
sys.argv[1:] => Check for argument which is given when starting the program
print("Text") => Print "Text" (with a linebreak \n at the end)
print("Text", end="-") => Print with end-statement (next print will be in the same line
print(repr("xyz\n")) => Print text with special characters like \n,\t etc
python test.py arg1 arg2 arg3 => example for starting a program with 3 parameters / arguments
len(sys.argv) => Len of the arguments - eg. 4 (program test.py itself and 3 arguments following)
min(3,5,7) => Find the minimum value => 3
max(3,5,7) => Find the maximum value => 7
stat.mean([3,5,7]) => Find the mean value => 5 (with statistic-module - input must be given as list)
sys.exit() => Stop program at this point (helpful in test-situations)
(newVal-oldVal)/oldVal * 100 => Calculate the change from an old value to a new value in percent
import pprint as pp => Import pretty print functionality
pp.pprint(dict) => pretty print dict line by line
pp.pprint(dict, width=30) => pretty print with max line-length 30
pp.pprint(nestedList,depth=1) => pretty print only level 1 of nested list eg. ['level1', [...]]
pp.pprint(nestedList,depth=2) => pretty print only level 2 of nested list eg. ['level1', ['level2', [...]]]
int("1010",2) => convert binary value to decimal
###### RANDOM, SECRETS
=> General
""
random: unsecure, reproducable with seeds
secret: secure, not reproducable
""
import random => Import random module - fast, but not very secure
random.randint(1,6) => Random int number between 1 and 6 like a cube
random.uniform(1,3) => Random float number between 1 and 3
random.random() => Random value between 0 and 1 in float format - eg. 0.16394553
random.uniform(1,10) => Random float in the range from 1 to 10
random.normal(0,1) => Random value for standard deviation
random.choice(list) => Choose random entry from a list
random.sample(list,3) => Choose 3 random (unique) entries from the list
random.choices(list,k=3) => Choose 3 random entries from the list (possible to be the same)
random.shuffle(l) => Shuffle the content of a list
random.seed(1) => Can reproduce the same results (unsecure)
"".join(random.choices(string.ascii_uppercase, k=6)) => generate a random uppercase 6-char string
import secrets => Import secrets module - for security reasons (is slower)
secrets.randbelow(10) => Random int in the range from 0-10 (10 excluded)
secrets.randbits(4) => Random int with 4 bits (highest possible value is 15 - 1111)
secrets.choice(l) => Random choice (which is not reproducable)
###### STRING
=> General
""
ordered, immutable, text representation
""
s = "this is a test" => Define a string
s = "this \n text" => String with linebreak \n
s = "this \t text" => String with tab between the words
=> Define string over more lines in editor with
""
s = '''this \
and this'''
""
s[0] => first char of a string
s[-1] => last char of a string
"text %s bla" % var => Insert variable in string oldest method (%s for string, %i for int, %f for float, %.2f for 2 decimals)
"text {} bla".format(var) => Insert variable in string old method (:.2f for 2 decimals)
f"text {v} bla" => Insert variable in string new method
f"{v:02d}" => Format int with allways 2 digits
f"{v:0.2f} => Format float with allways 2 decimal places
f"{v:09.2f} => Format float with 2 decmial places and overall 9 chars with leading 0
s = f"{v=}" => Output the variable name and the value of the variable in the form "v=4811"
"{0} like, {0} especially {0} but {1}".format("Joe", "noodles") => Multiple use
s.find ("ist") => Returns the index where the text is found (return -1 when nothing is found)
s.rfind("ist") => Returns the last index where the text is found (return -1 when nothing is found)
s.count ("i") => Counts the occurrence of a text
re.findall('[.,’]', s) => Counts several chars in a text
[i for i, c in enumerate(s) if c == "/"] => Return all index of the char "/" in the string
[x.start() for x in re.finditer('ist', s)] => Returns all index as a list for a substring in a string (needs import re)
s = s.lower() => Lowercase the whole string
s = s.upper() => Capitalize the whole string
s = s.capitalize() => Capitalize the first char
s = s.title() => Capitalize the first char of all words
s.startswith("H") => Check if string is starting with char "H"
s.startswith(("H","F","L")) => Check if string starts with several chars
s.endswith("H") => Check if string is ending with char "H"
s.endwith(("H","F","L")) => Check if string ends with several chars
s.split(" ") => Split the words in a list which are seperated by " "
re.split("[.|!|?]", s) => Split for several chars (eg. ".", "!", "?")
s.split('=>', 1)[0] => Split till the first occurence of "=>"
s.splitlines() => Split sentences after line breaks
s = s.strip() => Delete all whitespaces at the beginning and the end
s = " ".join(s.split()) => Remove all duplicate spaces in a string
s = s.replace("e","X") => Replacement of two strings
s = re.sub("[e0fi]", "X", s) => Replace several chars in a string
s2 = re.sub(f"[{string.punctuation}]", "", s) => Replace all punctuation chars in a string
s = re.subn("[e0fi]", "X", s) => Replace several chars in a string (get back a tupple: 1st changed string, 2nd: count of changed chars)
s.isdigit() => True if the whole string are digits
s.isalpha() => True if the whole string contains only letters
s2 in s => TRUE if string2 is in strings1
l = list(s) => Change string to list with all single chars
ord(char) => Convert char to ASCII value
chr(ascii) => Convert ASCII value to char (eg. 65=A, 97=a)
hashlib.md5(s).encode('utf-8')).hexdigest() => Outputs hashvalue oif the string (needs import hashlib)
eval("2+2") => Gives the value of an expression => 4 (dangerous - can used for sql-injections)
my_string[::-1] => Reverse a string
repr(s) => Print string with special characters
" ".join(s.split()) => Delete all blank, whitspaces from a string (method 1)
wAddress = re.sub('\s{2,}', ' ', s) => Delete all blank, whitspaces from a string (method 2)
=> check if any string is in another string or all strings are in another string
""
s = "A string is more than its parts!"
l = ["more", "blabla", "nothing"]
if any(x in s for x in l): # check if any elems from checks in string s
if not any(x in s for x in l): # check if any elems from checks are NOT in string s
if all(x in s for x in l): # check if all elems from checks are in string s
""
=> add linebreak after numer of words
""
start = 0
N = 3
l = inp.split()
outText = []
for stop in range(N, len(l)+N, N):
worker = ' '.join(l[start:stop])
outText.append(worker)
start = stop
outText = "\n".join(outText)
""
=> string library
""
https://docs.python.org/3/library/string.html
import string
string.ascii_letters => all letters
string.ascii_uppercase => uppercase letters
string.ascii_lowercase => lowercase letters
string.digits => digits
string.punctuation => punctuation
""
###### REGEX
import re => import regex module
https://medium.com/factory-mind/regex-tutorial-a-simple-cheatsheet-by-examples-649dc1c3f285 => Information about regex handling
https://regex101.com/ => test regex strings
pattern = re.compile("^[0-9]{4}-[0-9]{2}$") => Find str with 4xdigits + "-"char + 2xdigits
pattern.match(s) => Check if pattern matches - <> None when matches
pattern2 = re.compile("[0-9]{4}-[0-9]{2}") => Find str with 4xdigits + "-"char + 2xdigits
pattern2 = re.compile("[0-9]{3,4}-[0-9]{2}") => Find str with 3 to 4 xdigits + "-"char + 2xdigits
pattern.fullmatch(s) => Check with fullmatch (^ and $ not necessary) - <> None when matches
pattern3 = re.compile ("[0-9]{1}\.[0-9]{3}") => Char "." has to cherck with "\n" (is a wildcard letter in regex)
re.sub("\d","",s) => Replace all digits in string with blank
re.sub(r'([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))', r'\1 ', "txt") => Insert blank before every capitalized word eg. "CostOfRevenue" => "Cost Of Revenue"
len(re.findall(f'[{cL}"]{w}[{cL}"]', l)) => count all words which start and ends with a char from cL
len(re.findall(f'^{w}[{charList}"]', l)) => count all words which start with w and ends with a char from cL
len(re.findall(f'[{charList}"]{w}$', l)) => count all words which start with a char from cL and ends w
len(re.findall(f'(.*?){w}(.*?)', l)) => count all words which have w in the middle of anything else
=> Read string an try to match with groups
pattern = "([0-9]+) ([a-z]:([a-z]+)" => Define pattern => as many digits (endless) + " " + one lowercase cahr + ":" + as many digits (endless)
match = research(pattern, elem) => Check if elem fits the pattern => returns <re.Match object; span=(0, 19), match="3 n: nnnlncrnnnnn">
value = match.group(1) => Assign the value of the first group => 3
all = match.group(0) => Show the full string => "3 n: nnnlncrnnnnn"
###### LIST
=> ordered, mutable, allows duplicate elements
l=[x*2 for x in l] => Also possible with list comprehension => [2,4,6,8,10]
l=[x for x in l if x%2==0] => Also possible with list comprehension => [2,4]
l=[x*2 if x.isdigit() else x for x in l] => List comprehension with if and else
l1,l2,l3,l4 = ([] for i in range(4)) => Define several empty lists (NOT use l1=l=l3=[] => this would be the SAME list)
if l => check for True if list has an element
if not l => check for False if list is empty (same would be if l == []
l = [4,5,6] => Define list with content
l = list(range(20)) => Define list - content is list from 0 to 19
l = ["" for x in range(8)] => Define list with 8 empyt strings
[x.upper() for x in l] => Uppercase the whole list
l = [["" for x in range(5)] for x in range(5)] => Define a nested list with 5x5
l1 + l2 => Add both lists together (to a new list)
l.append(1) => Add 1 element to the list at the back
l.extend([6,5,4]) => Add several elements to the list at the back
l.insert(3, "xyz") => Insert an element at the index-position 3
l.index("xzy") => Returns first index position of an element
[i for i,v in enumerate(wHeader) if v == "xyz"] => Find all indizes for a specific element
l.pop() => Delete last element from list
l.pop(0) => Delete first element from list
l.sort() => Sort list ascending
l.sort(reverse=True) => Sort list descending
l_sort = sorted(l) => Sort list and store it in a different independent list
l_sort = sorted(l, reverse=True) => Sort list descending and store it in a different independent list
l_sort = os_sorted(l) => Sort the same way as the operating system (need: from natsort import os_sorted)
if sorted(l) == l => Check if a list is sorted (by ascending)
if sorted(l, reverse=True) == l => Check if a list is sorted by descending
l.sort(key=len) => Sort list by len of elements
l = [[1,4,3], [2,2,4], [3,1,5]] => Define nested list
l.sort(key=lambda x: x[1]) => Sort nested list for the 2nd element ascending
l.sort(key=lambda x: x[1], reverse=True) => Sort nested list for the 2nd element descending
l.sort(key=lambda x: (x[0], x[1])) => Sort nested list 1st for the 1st elem, then for the 2nd elem
l.sort(key=lambda x: (x[0], x[1]), reverse=True) => Sort nested list 1st for the 1st elem, then for the 2nd elem descending
l.reverse() => Reverse the complete list
l_reverse = list(reversed(l)) => Reverse complete list and store it in different independent list
del l[2] => Delete element at index position 2
l.remove("abc") => Delete first element with this value from the list
l.clear() => Delete complete content of the list (same as l = [])
min(l) => Find smallest element in the list
max(l) => Find greatest element in the list
max(mylist, key=len) => Find longest string in the list
len(max(mylist, key=len)) => Find lenght of longest string in the list
sum(l) => Sum of all elements in the list
[sum(pair) for pair in zip(l1, l2)] => Pair Sorting from 2 lists (1 with 1, 2 with 2, 3 mit 3, aso.)
len(l) => Count of elements in list
l.count("a") => Count of occurence of the element "a" in the list
l2 = l1 => No seperate copy of the list (updates in both lists)
l2 = l.copy() => Separate individual copy of the list (no updates in both lists)
l2 = l[:] => Separate individual copy of the list - 2nd variant
l2 = list(l) => Separate individual copy of the list - 3rd variant
l2 = copy.deepcopy(l) => Seperate copying a nested list - import copy necessary
l[0] => First element
l[-1] => Last element
l[-3:] => Last 3 elements
l[2:4] => Elements from index position 2 to 3
l[:2] => Elements from 0 to 1 (exclusive index position 2)
l[2:] => Elements from index position 2 to the end of the list
l[::2] => Every second element [start:end:step]
l[::-1] => Reverse list
9 in l => Check if element is in list
if any(x in st for x in ["ab","cd","de"]): => Check if any elements from the list are in the string
if all(x in st for x in ["ab","cd","de"]): => Check if all elements from the list are in the string
if all(x not in str for x in ["ab","cd"]): => Check if all elements from the list are NOT in the string
for i in l: => Iterate through list content
for i in range(len(l)): => Iterate through list with index
for idx, cont in enumerate(l): => Iterate through list with index and content
for x,y in zip(l1,l2): => Iterate through 2 lists pair-wise (stops when the shorter list is reached)
', '.join(l) => Create string with elements joined together with ", " eg. ["a","b","c"] => a, b, c
a = [1,2,3,4,5] => Define list
list(map(lambda x: x*2, [1,2,3,4,5])) => List is mapped with the lambda function => [2,4,6,8,10]
list(filter(lambda x: x%2==0, [1,2,3,4,5])) => List is filtered with lambda for even numbers => [2,4]
zip([1,2,3],[4,5,6]) => Build paris as tuple => (1,4),(2,5),(3,6)
list(set(l)) => Remove duplicates from a list
list(dict.fromkeys(l)) => Remove duplicates from a list and keep existing order
list(map(int,l)) => Change elements in list to int
lNew = [list(x) for x in zip(*l)] => Transpose nested list (change rows and columns)
l3 = set(l2) - set(l1) => Get difference of 2 lists
1st, 2nd, *rest = ["A","B","C","D","E"] => Unpacking of a list (1st=A, 2nd=B, rest=["C","D","E"])
[l[i:i + C] for i in range(0, len(l), C)] => Split a list (l) in chunks (C)
=> rotate list for n places
""
def rotate(l, n):
return l[-n:] + l[:-n]
""
=> Iterate over 2 lists at the same time using zip
""
animal = ['Cat', 'Dog', 'Fish', 'Goat']
age = [1, 2, 2, 6]
z = zip(animal, age)
for animal, age in z: ...
""
=> Adjust nested list to the same length
""
maxLen = max(map(len, l))
for row in l:
while len(row) < maxLen:
row.append(None)
""
=> Sort list in a specific value-order for eg. the sixth column in the list
""
SORT_ORDER = {"RUSH": 0, "HIGH": 1, "MEDIUM": 2, "LOW": 3, "FINISHED": 4}
l.sort(key=lambda val: SORT_ORDER[val[5]])
""
###### TUPLE
=> ordered, immutable, allows duplicate elements
=> working can be more efficient than with lists - especially with big data - regarding space and runtime
t = (4,5,6) => Create a tuple
t = tuple([3,4,4]) => Create a tuple from a list
t[] => Selection / slicing of elements - same as with lists
l = list(t) => Change tuple to list
t = tuple(l) => Change list to tuples
a,b,c = (0,1,2) => Assigning vars to tuple-elements (a=0, b=1, c=2)
f,*m,l = (0,1,2,3,4) => Assigning to tuple-values (f=0, m=[1,2,3], l=4)
###### DICTIONARY
=> key-value pairs, unordered (till version 3.7), mutable
d = {} => Define empty dict
d = {"one": 1, "two": 2, "three": 3} => Define dict with content
d = dict(one=1,two=2,three=3) => 2nd way to defince a dict with content
d = dict([("one",1),("two",2),("three",3)]) => Define dict with content (with dupels)
d = dict([["one",1],["two",2],["three",3]]) => Define dict with content (with pairs in nested list)
d = dict(zip(["one","two","three"], [1,2,3])) => Define dict with 2 different lists (1x keys and 1x values)
d["two"] => Access value with key element (error when the key is not in the dict)
d.get("two","N/A") => BETTER: Acesss value with get for key element (no error when the key is not in the dict - returns second parameter instead)
d.setdefault("two","N/A") => Access value - same as get - but also initialize the key when it is not in the dict - with the second parameter)
key = list(d.keys())[list(d.values()).index(v)] => Find key for specific value (v) in dict
list(d.keys()) => Read keys from dict to list
list(d.values()) => Read values from dict to list
len(d) => Count of entries in dict
"three" in d => Check if key is in dict (true / false)
d["four"] = 4 => New entry for dict (key = "four", value = 4)
del d["one"] => Delete specific key in dict
mydict[new_key] = mydict.pop(old_key) => Rename dict-keyname
combined_dict = {**d1, **d2} => Combine 2 dicts (if key is in both dicts - the second value will be taken)
if "xyz" in d: => Check if key is in dict
d2 = d => Copying a dict (all changes will be made in BOTH dicts)
d2 = d.copy() => Copying a dict (NOT WORKING with nested lists in the dict - use deepcopy!)
d2 = dict(d) => Copying a dict (NOT WORKING with nested lists in the dict - use deepcopy!)
d2 = copy.deepcopy(d) => Copying a dict (dicts will be handled seperate) - import copy necessary
d.update(d2) => Dict d get updated with d2 (all existing keys are overwritten - and new added)
for key in d.keys(): => Iterate through dict keys
for key in sorted(d.keys()): => Iterate through sorted dict keys ascending
for key in sorted(d.keys(),reverse=True): => Iterate through sorted dict keys descending
for val in d.values(): => Iterate through dict values
for key, val in d.items(): => Iterate through keys and values of the dict
d = {x[0]: x[1:] for x in l} => built dict with dict-comprehension
d = {k: v for k, v in sorted(d.items(), key=lambda x: x[1], reverse=True)} => Sort dict descending according to values (=item[1])
d = {k: v for k, v in sorted(d.items(), key=lambda x: x[0])} => Sort dict ascending according to keys (=item[0])
d = {k: v for k, v in sorted(d.items(), key=lambda x: (-x[1],x[0]))} => Dict sorted: 1st value-desc (x[0]) - 2nd key-ascnd (-x[0])
l = sorted(d.items(), key=lambda e: e[1][2]) => Sort dict and output list, sorted according to the 3rd element of the list in the dict
l = sorted(d.items(), key=lambda e: e[1][2], reverse=True) => Sort dict and output list, sorted according to the 3rd element of the list in the dict - descending
d3 = {key: d1.get(key,0)+d2.get(key,0) for key in set(d1)|set(d2)} => Merge 2 dictionaries
=> save dict to pkl-file
""
import pickle
with open("fn.pkl", "wb") as f:
pickle.dump(dict, f)
""
=> read dict from pkl-file
""
import pickle
with open('fn.pkl', 'rb') as f:
d = pickle.load(f)
""
###### SETS
=> unordered, mutable, no duplicates
s=set() => Define empty set - {} would define a dict and not a set
s = {1,1,2,2,3,4} => Define set with content
s = set("Hello") => Define empty set - returns: {"o","l","H","e"}
s2 = {1,7,8} => Define second set
s.add(5) => Add element in set1
s.update([10,11,12]) => Add several elements to set1
s & s2 => Intersection of 2 sets (same as: s.intersection(s2))
s | s2 => Untion of 2 sets (same as: s.union(s2))
s - s2 => Difference of 2 sets (same as: s.difference(s2))
s <= s2 => True if s is subset from s2 (same as: s.issubset(s2))
3 in s => Check if element is in set
s.clear() => Clear the set
s.pop() => Delete lowest elmeent in set
s.remove(5) => Delete element 5 from set - but key error possible
s.discard(5) => Delete element 5 from set - NO key error possible
for i in s: => Iterate through set content
s2 = s1 => Copying a set (all changes will be made in BOTH sets)
s2 = s.copy() => Copying a set (set will be handled seperate)
s2 = set(s) => Copying a set 2nd method (set will be handled seperate)
s = frozenset(1,2,3) => Define a frozenset - no changes are possible in the set - union, intersection aso. will work
s = {x for x in l} => Create a set with set-comprehension form a list as basis
###### CONTROL STRUCTURES and ITERATIONS
=> If / elif / else
""
if x > 10: pass
elif x > 10: pass
else: pass
""
if (10 < a < 20)... => Use 2 conditions in one line (instead of (a>10 and a<20)
if a<10 & b>10 & c==4... => Connecting different control structures with logical and &
for i in range(5): => 5 iterations from 0 to 4
for i in range(0, 5, 1) => 5 iterations from 0 to 4 (start, end, step)
for i in range(4, -1, -1) => Iterations descending from 4 to 0
while x < 4: => While loop with break condition
while True: => Endless while loop - has to be exited with break
break => Break loop completely
continue => Break actual loop run - continue with next loop run
###### FUNCTIONS, DECORATORS
=> description of the function in form of a docstring
""
def printNumAbbr(value):
'''
eg. Make abbreviaton for numeric value in thousands (K), millions (M), billions (B) or trillions (T)
:param value: numeric value which should be abbreviated
:return: string value with maximum possible abbreviation
'''
""
=> Define function - with default value 0 if no input is given
""
def add(x=0,y=0):
erg = x+y # Calculation in function
return erg # Return value from the function
""
=> Optional argument in the funtction (first element is must - second optional)
""
def pet (animal,n1=None,n2="x")
pet("Cat") # Calling function with n1=None and n2 = "x"
pet("Cat","name") # Calling function with n1=name and n2 = "X"
pet("Cat",n2="xyz") # Calling function with n1=None and n2 = "xyz" (third parameter has to be named when calling)
""
=> Function with infinite arguments
""
def varargs(*args): print(args)
varargs(1,2,3) # Outputs (1,2,3)
def keyword_args(**kwargs):
print(kwargs)
keyword_args("a"=3, "b"=4) # Outputs {"a":3, "b":4}
""
=> decorator template
""
def my_decorator(func): # Define decorator
@functools.wraps(func)
def wrapper(*args,**kwargs): # Decorator with * arguments
#Do... # Do something before the functions
result=func(*args,**kwargs) # Run the function
#Do... # Do something after the functions
return result # Return the results from the function
return wrapper
""
=> decorator
""
extend behaviour of a function with a decorators
def start_end_decorator(func): # Define the decorator with function "func" as input-parameter
def wrapper(): # Inside the decorator a wrapper function has to be defined
print("Start") # Decorated code which is executed before the core code from the function
func() # Calling the function itself
print("End") # Decorated code which is executed after the core code from the function
return wrapper # Results from the decorator have to be given back
@start_end_decorator # Defines this decorator for the following function "print_name"
def print_name(): # Normal content of the function
print("xyz") # Core functionality of the function
print_name() # Now when the function is executed - outputs not only "xyz" - also "Start" before and "End" after
""
=> decorator with function arguments
""
def start_end_decorator(func): # Define the decorator with function "func" as input-parameter
def wrapper(*args,**kwargs): # Inside the decorator a wrapper function has to be defined
print("Start") # Decorated code which is executed before the core code from the function
result = (*args,**kwargs) # Calling the function itself
print("End ") # Decorated code which is executed after the core code from the function
return result
return wrapper # Results from the decorator have to be given back
@start_end_decorator # Defines this decorator for the following function "add5"
def add5(x): # Normal content of the function with one argument
return x + 5 # Core functionality of the function
result = add5(10) # Outputs "Start" => "End" => 15
print(result)
""
###### GENERATORS, YIELD
=> very memory efficient
=> Defines the generator with 3 yield statements
""
def mygenerator():
yield 3
yield 2
yield 1
""
g = mygenerator() => Creates the generator and stores in g (=generator type)
for i in g: print(i) => Outputs 3,2,1
print(next(g)) => Outputs 3
print(next(g)) => Outputs 2
sum(g) => Sum-Function can also use a generator => result is 6
sorted(g) => Sorted-Function can use generator => returns list with sorted elements [1,2,3]
###### EXCEPTIONS
=> try / except
""
try:
a = 5 / 0
except FileNotFoundError as error:
print(f"File not found with this error:)
print("Exception: ",error) # Prints exception "division by zero"
except Exception as error:
print(f"Problems finding file {fn} - skipped...")
continue
""
=> assert / raise
""
x = -5
assert (x>=0), "x is not positive" # Checks / Assert some condition => prints "x is not positive"
if x<0: raise Exception("x should positive") # Raises an exception => prints "Exception: x should be positive"
""
###### LAMBDA, MAP, FILTER
add10 = lambda x: x+10 => functions with one argument - add10(5) => 15
mult = lambda x,y: x*y => functions with two arguments for multiplying - mult(2,7) => 14
=> p = [(1,2),(15,1),(5,-1),(10,4)]
sorted(p) => output is sorted with first element then second element => [(1,2),(5,-1),(10,4),(15,1)]
sorted(p,key=lambda x:x[1]) => output is sorted by the second element => [(5,-1),(15,1),(1,2),(10,4)]
sorted(p,key=lambda x:x[0]+x[1]) => output is sorted by the sum of both => [(1,2),(5,-1),(10,4),(15,1)]
=> a = [1,2,3,4,5]
list(map(lambda x: x*2,a)) => list is mapped with the lambda function => [2,4,6,8,10]
c=[x*2 for x in a] => also possible with list comprehension => [2,4,6,8,10]
list(filter(lambda x: x%2==0,a)) => list is filtered with lambda for even numbers => [2,4]
c=[x for x in a if x%2==0] => also possible with list comprehension => [2,4]
###### CLASSES, DATACLASSES
=> Define a class
""
class Human(object):
species = "Homo Sapiens" # Fix variable / class variable for all instances of the class
def __init__(self,name): # Constructor - automatically applied when an instance is created
self.name = name # Name is assigned to the instance of the classe (self.)
self.tresor = [] # Tresor is assigned as list to the instance of the class (self.)
def say(self, msg): # Methode of the class
return "{name}: {message}".format(name=self.name, message=msg)
elf.tresor.append(msg) # Tesor of the instance gets a new value in the list
@classmethod # Class methode - is used by all instances
def get_species(cls):
return cls.species
@staticmethod # Static methode - is called without class or method
def grunt():
return "*grunt*"
""
i = Human(name="Ian") => Create instance of the class
print(i.say("Hi")) => Call the methode of the class (output: "Ian: Hi")
j = Human(name="Joel") => Create additonal instance of the class
print(i.say("Hallo")) => Call the methode of the class (output: "Joel: Hallo")
i.get_species() => Output "Homo Sapiens"
j.get_species() => Same output "Homo Sapiens"
Human.species = "Was Neues" => Change of the class variable - applies for all instances
Human.grunt() => Aufruf der statischen Methode => Ausgabe: "*grunt*"
=> Dataclasses
""
from dataclasses import dataclass # import dataclass necessary
@dataclass # define a dataclass
class Coordinate: # class is defined an need NO __init__, __repr__, __eq__
x: int # __init__ no necessary - values get assigned automatically
y: int = 10 # __repr__ print for the string-represantion is automatic => Coordinate(x=4, y=5)
a = Coordinate(4, 5)
@dataclass (frozen=True) # defines a unmutable instance
asdict(dc) # converts the dataclass to a dict
astuple(dc) # converts the dataclass to a tuple
""
###### TXT FILES
with open ("fn.txt","r") as f: print(f.read() => Read textfile - and print it
with open ("fn.txt","r+") as f: print(f.read() => Open File for Read and Write
=> Read textfile - stored in list per line
""
with open(fn, encoding="mac_roman", errors="ignore") as f:
lines = f.read().splitlines()
""
=> Read json textfile to dict
""
with open(fn, encoding="mac_roman", errors="ignore") as data:
info = json.load(data)
""
=> Write txtfile - from list
""
fn = "".join(i for i in wTitle if i not in "\/:*?<>|" and i not in '"')
with open(fn, "w", encoding="mac_roman", errors="ignore") as f:
lines = f.writelines(["Hello\n", "World", "NextLine"])
""
=> Write txtfile - from string
""
fn = "".join(i for i in wTitle if i not in "\/:*?<>|" and i not in '"')
with open(fn, "w", encoding="mac_roman", errors="ignore") as f:
f.write("some text")
""
=> Write only a max-size in a txt-file (eg. only a maximum of 50kb will be written)
""
maxSize = 50*1024 # 50kb in bytes
totalSize = 0
fn = "".join(i for i in wTitle if i not in "\/:*?<>|" and i not in '"')
with open(fn, 'w', encoding='utf-8') as f:
for l in foundText.split("\n"):
sizeLine = len(l.encode('utf-8'))
if totalSize + sizeLine > maxSize:
print("Reached the maximum file size limit. Stopping further writes.")
break
f.write(l)
totalSize += sizeLine
""
with open("fn.txt","a") as obj: obj.write("\nNoch ein Text") => Append text in the next line
if os.path.exists(fn) == False: => Check if file allready exists
with open (fn,"a") as f: f.write("init") => Create and initialize file when not exisiting
f.read() => Read content to string
f.seek(0) => Set the current postion in the file to beginning
f.write(s) => Write s to the f-file opened
=> Try/Except - checks if file can be saved
""
while True:
try: # otherwise outputs a error message
writer.save ()
break
except Exception as e:
print(f"Error happened: {e}")
traceback.print_exc() # Outputs the detailed error message
input ("File Open not possible - pls close and press <Enter>")
""
###### JSON FORMAT
import json => Import json-module
fn = "numbers.json" => Assign JSON-filename
with open(fn) as data: info = json.load(data) => Convert json to dict
json_format = json.dumps(d,indent=2) => Convert / Encode a dict to a json-file (with indent for better reading)
json_format = json.dumps(d,sort_keys=True) => Convert / Encode a dict to a json-file (with sorting the keys)
person = json.loads(json_format) => Convert / Decode a json-file to a dict
with open(fn.json,"r") as data: d=json.load(file) => Reading information in json-format
print(json.dumps(erg, indent=4, sort_keys=True)) => Pretty-Print formatted json-file
=> UTF8-errors
=> when there are utf-8 errors pls try to set the system variables