Python 3.7.3 | packaged by conda-forge | (default, Jul 1 2019, 21:52:21)
Type "copyright", "credits" or "license" for more information.
IPython 7.8.0 -- An enhanced Interactive Python.
In [1]: ls
accettazione-gianluca.pdf lecture08.mp4
agenda.py* lettera-gianluca.pdf*
albero-dir.py* Libri/
albero-vettore.py* Lightworks/
anaconda.3.7 lmms/
ANALISI-PA@ Maps/
Arduino/ mips11.asm*
artists/ mips1.asm
bin/ missfont.log*
birdseye/ Music/
bollettino-caldaia-2018.pdf* My_Cmaps/
'Calibre Library'/ my-systemd.pp
cloud-config.yml my-systemd.te*
cltk_data/ n
CmapToolsLogs/ nltk_data/
codifica.py* npm-debug.log
conf.py OPT-IEETEL-symmetric.ods*
consegna-gianluca.pdf oripa.ini
CS-methods/ PDF/
CUDATest.nb* pgadmin.log
Desktop/ Pictures/
docker-snippets pixiedust/
Documents/ "PlayOnLinux's virtual drives"@
Downloads/ pro.py*
Drive/ prova.py*
Dropbox/ Public/
DUPES/ qrcodegen/
dwhelper/ Recordings/
emu/ referee-JSEE-2019-48.txt
euromoot-2019-acconto.pdf rosegarden/
f1.gephi rpmbuild/
fcdtdebugger/ rubik.txt
filippo.gephi* snap/
f.py src/
GCloud/ ssl/
genymotion-logs-20190313-215932.zip stanfordnlp_resources/
git/ STEM/
GPUCache/ Templates/
gurobi.log tmp/
hacked/ TODO/
HACKS* Videos/
HW@ viva.py*
index.html wekafiles/
index.rst* workspace/
iso/ workspace-ARM/
knime-workspace/ workspace-fcdtdebug/
LDCad/
In [2]: dir
Out[2]: <function dir>
In [3]: ls
26/ lezione2.py
26.py lezione3-ipython-console.html
54/ lezione3.py
54.py lezione4-ipython-console.html
'ANALISI E PROGETTO.txt' lezione4.py
'FILE E WEB.txt' lezione5-ipython-console.html
files/ lezione5.py
files.zip lezione6-ipython-console.py
lezione1-ipython-console.html lezione6.py
lezione1.py lezione7.py
lezione2-ipython-console-2.html __pycache__/
lezione2-ipython-console.html
In [4]: cwd
Traceback (most recent call last):
File "<ipython-input-4-e863a2ac204e>", line 1, in <module>
cwd
NameError: name 'cwd' is not defined
In [5]:
In [5]: pwd
Out[5]: '/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni'
In [6]: ls files
alice_it.txt frankenstein.txt log.log prince.txt testo2.txt words.py
alice.txt holmes.txt logo.png results.txt testo.txt
In [7]: open("files/alice.txt")
Out[7]: <_io.TextIOWrapper name='files/alice.txt' mode='r' encoding='UTF-8'>
In [8]: F = open("files/alice.txt")
In [9]: testo = F.read()
In [10]: len(testo)
Out[10]: 163780
In [11]: testo[1000:1030]
Out[11]: "es or\nconversation?'\n\nSo she w"
In [12]: import os
In [13]: os.linesep
Out[13]: '\n'
In [14]: F.read()
Out[14]: ''
In [15]: F.seek(0)
Out[15]: 0
In [16]: F.readline()
Out[16]: "\ufeffProject Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n"
In [17]: F = open("files/alice.txt", encoding='utf-8-sig')
In [18]: F.readline()
Out[18]: "Project Gutenberg's Alice's Adventures in Wonderland, by Lewis Carroll\n"
In [19]: F.readline()
Out[19]: '\n'
In [20]: F.readline()
Out[20]: 'This eBook is for the use of anyone anywhere at no cost and with\n'
In [21]: F.tell()
Out[21]: 143
In [22]: F.close()
In [23]: F.readline()
Traceback (most recent call last):
File "<ipython-input-23-a32c22ac6d1f>", line 1, in <module>
F.readline()
ValueError: I/O operation on closed file.
In [24]:
In [24]: with open("files/alice.txt", encoding='utf-8-sig") as F:
...: testo = F.read()
File "<ipython-input-24-0ba2fdba16c6>", line 1
with open("files/alice.txt", encoding='utf-8-sig") as F:
^
SyntaxError: EOL while scanning string literal
In [25]:
In [25]: with open("files/alice.txt", encoding='utf-8-sig') as F:
...: testo = F.read()
...:
In [26]: F.readline()
Traceback (most recent call last):
File "<ipython-input-26-a32c22ac6d1f>", line 1, in <module>
F.readline()
ValueError: I/O operation on closed file.
In [27]:
In [27]: with open("files/alice.txt", encoding='utf-8-sig') as F:
...: for i, linea in enumerate(F):
...: if "Alice" in linea:
...: print(i)
...:
0
8
41
44
53
59
65
69
85
93
98
112
117
127
130
138
143
151
156
160
167
171
182
193
201
207
215
222
228
235
253
261
279
283
294
300
335
343
356
367
375
386
390
397
399
403
409
412
416
424
426
432
440
446
460
463
470
504
506
521
546
550
558
560
568
579
583
586
637
640
645
651
656
666
672
682
687
702
709
712
725
727
730
759
764
774
778
787
793
794
797
822
827
844
862
868
871
874
876
882
893
897
910
918
921
929
933
941
959
965
973
978
984
991
997
1003
1010
1014
1019
1025
1031
1035
1080
1090
1095
1101
1106
1113
1120
1125
1149
1155
1171
1177
1183
1190
1198
1203
1212
1219
1224
1229
1245
1261
1274
1279
1288
1294
1308
1313
1321
1325
1333
1342
1348
1357
1362
1371
1378
1385
1408
1419
1424
1426
1435
1438
1441
1444
1450
1454
1469
1475
1480
1484
1489
1496
1501
1505
1510
1516
1521
1526
1533
1538
1548
1555
1576
1580
1581
1586
1591
1596
1599
1602
1608
1614
1618
1632
1637
1641
1656
1663
1668
1678
1681
1687
1693
1698
1710
1716
1728
1744
1749
1755
1759
1765
1776
1785
1791
1796
1800
1802
1808
1812
1819
1824
1831
1839
1841
1844
1851
1856
1863
1867
1876
1881
1888
1913
1935
1939
1950
1959
1965
1972
1977
1979
1989
1995
2019
2021
2033
2036
2038
2044
2051
2055
2058
2066
2071
2082
2091
2106
2108
2113
2124
2130
2133
2142
2144
2157
2165
2167
2172
2182
2198
2215
2218
2237
2240
2243
2245
2255
2259
2263
2269
2275
2280
2283
2287
2296
2303
2308
2315
2321
2323
2334
2348
2351
2354
2358
2362
2371
2372
2378
2380
2385
2390
2404
2413
2422
2429
2435
2439
2444
2449
2453
2459
2465
2471
2476
2481
2490
2504
2510
2515
2521
2532
2539
2540
2544
2578
2580
2584
2591
2621
2628
2634
2647
2653
2657
2663
2669
2674
2681
2686
2692
2700
2714
2720
2742
2753
2759
2777
2783
2815
2818
2837
2842
2851
2860
2866
2871
2875
2877
2943
2952
2956
3016
3030
3046
3077
3081
3086
3089
3104
3106
3120
3122
3126
3140
3147
3149
3155
3160
3203
3249
3272
3294
3299
3304
3313
3316
3321
3325
3329
3374
In [28]: with open("files/alice.txt", encoding='utf-8-sig') as F:
...: numeri = []
...: for i, linea in enumerate(F):
...: if "Alice" in linea:
...: numeri.append(i)
...:
In [29]: numeri
Out[29]:
[0,
8,
41,
44,
53,
59,
65,
69,
85,
93,
98,
112,
117,
127,
130,
138,
143,
151,
156,
160,
167,
171,
182,
193,
201,
207,
215,
222,
228,
235,
253,
261,
279,
283,
294,
300,
335,
343,
356,
367,
375,
386,
390,
397,
399,
403,
409,
412,
416,
424,
426,
432,
440,
446,
460,
463,
470,
504,
506,
521,
546,
550,
558,
560,
568,
579,
583,
586,
637,
640,
645,
651,
656,
666,
672,
682,
687,
702,
709,
712,
725,
727,
730,
759,
764,
774,
778,
787,
793,
794,
797,
822,
827,
844,
862,
868,
871,
874,
876,
882,
893,
897,
910,
918,
921,
929,
933,
941,
959,
965,
973,
978,
984,
991,
997,
1003,
1010,
1014,
1019,
1025,
1031,
1035,
1080,
1090,
1095,
1101,
1106,
1113,
1120,
1125,
1149,
1155,
1171,
1177,
1183,
1190,
1198,
1203,
1212,
1219,
1224,
1229,
1245,
1261,
1274,
1279,
1288,
1294,
1308,
1313,
1321,
1325,
1333,
1342,
1348,
1357,
1362,
1371,
1378,
1385,
1408,
1419,
1424,
1426,
1435,
1438,
1441,
1444,
1450,
1454,
1469,
1475,
1480,
1484,
1489,
1496,
1501,
1505,
1510,
1516,
1521,
1526,
1533,
1538,
1548,
1555,
1576,
1580,
1581,
1586,
1591,
1596,
1599,
1602,
1608,
1614,
1618,
1632,
1637,
1641,
1656,
1663,
1668,
1678,
1681,
1687,
1693,
1698,
1710,
1716,
1728,
1744,
1749,
1755,
1759,
1765,
1776,
1785,
1791,
1796,
1800,
1802,
1808,
1812,
1819,
1824,
1831,
1839,
1841,
1844,
1851,
1856,
1863,
1867,
1876,
1881,
1888,
1913,
1935,
1939,
1950,
1959,
1965,
1972,
1977,
1979,
1989,
1995,
2019,
2021,
2033,
2036,
2038,
2044,
2051,
2055,
2058,
2066,
2071,
2082,
2091,
2106,
2108,
2113,
2124,
2130,
2133,
2142,
2144,
2157,
2165,
2167,
2172,
2182,
2198,
2215,
2218,
2237,
2240,
2243,
2245,
2255,
2259,
2263,
2269,
2275,
2280,
2283,
2287,
2296,
2303,
2308,
2315,
2321,
2323,
2334,
2348,
2351,
2354,
2358,
2362,
2371,
2372,
2378,
2380,
2385,
2390,
2404,
2413,
2422,
2429,
2435,
2439,
2444,
2449,
2453,
2459,
2465,
2471,
2476,
2481,
2490,
2504,
2510,
2515,
2521,
2532,
2539,
2540,
2544,
2578,
2580,
2584,
2591,
2621,
2628,
2634,
2647,
2653,
2657,
2663,
2669,
2674,
2681,
2686,
2692,
2700,
2714,
2720,
2742,
2753,
2759,
2777,
2783,
2815,
2818,
2837,
2842,
2851,
2860,
2866,
2871,
2875,
2877,
2943,
2952,
2956,
3016,
3030,
3046,
3077,
3081,
3086,
3089,
3104,
3106,
3120,
3122,
3126,
3140,
3147,
3149,
3155,
3160,
3203,
3249,
3272,
3294,
3299,
3304,
3313,
3316,
3321,
3325,
3329,
3374]
In [30]: numeri[:10]
Out[30]: [0, 8, 41, 44, 53, 59, 65, 69, 85, 93]
In [31]: open("pippo.txt")
Traceback (most recent call last):
File "<ipython-input-31-fea486396c80>", line 1, in <module>
open("pippo.txt")
FileNotFoundError: [Errno 2] No such file or directory: 'pippo.txt'
In [32]:
In [32]: " lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n".strip()
Out[32]: 'lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l'
In [33]: " lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n".lstrip()
Out[33]: 'lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n'
In [34]: " lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n".rstrip()
Out[34]: ' lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l'
In [35]: " lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n".split()
Out[35]: ['lkfdjgòlsdkjgò', 'ksajfhv', 'lkjlkjb', 'lkjh', 'l']
In [36]: " lkfdjgòlsdkjgò ksajfhv lkjlkjb lkjh l \n".split(' ')
Out[36]: ['', 'lkfdjgòlsdkjgò', 'ksajfhv', 'lkjlkjb', 'lkjh', 'l', '', '', '', '', '\n']
In [37]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni/lezione7.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni')
In [38]: L = trova_parola('files/alice.txt', 'tuRtle', enc='utf-8-sig')
In [39]: %pprint
Pretty printing has been turned OFF
In [40]: L
Out[40]: [2212, 2352, 2354, 2356, 2370, 2388, 2395, 2401, 2408, 2409, 2413, 2418, 2420, 2424, 2430, 2437, 2440, 2447, 2451, 2455, 2462, 2467, 2482, 2484, 2492, 2498, 2507, 2519, 2531, 2535, 2549, 2558, 2562, 2570, 2576, 2582, 2586, 2593, 2599, 2625, 2631, 2637, 2639, 2678, 2683, 2688, 2695, 2706, 2711, 2739, 2745, 2750, 2773, 2781, 2783, 2785, 2788, 2811, 3346, 3356]
In [41]: L2 = trova_parola_lc('files/alice.txt', 'tuRtle', enc='utf-8-sig')
In [42]: L == L2
Out[42]: True
In [43]: isalpha('A')
Traceback (most recent call last):
File "<ipython-input-43-84c1c2b1d9f6>", line 1, in <module>
isalpha('A')
NameError: name 'isalpha' is not defined
In [44]:
In [44]: 'a'.isalpha()
Out[44]: True
In [45]: '0'.isalpha()
Out[45]: False
In [46]: 'A'.isalpha()
Out[46]: True
In [47]: 'è'.isalpha()
Out[47]: True
In [48]: t = "òeirtup ouh45125p1iuht òkjhgòkjwrhfq...222"
In [49]: set(t)
Out[49]: {'f', '4', 'w', 'k', 'q', 'h', 'ò', 'o', '1', '.', '2', 'p', 'j', 't', '5', 'e', 'u', 'r', ' ', 'i', 'g'}
In [50]: [ c for c in set(t) if c.isalpha() ]
Out[50]: ['f', 'w', 'k', 'q', 'h', 'ò', 'o', 'p', 'j', 't', 'e', 'u', 'r', 'i', 'g']
In [51]: [ c for c in set(t) if not c.isalpha() ]
Out[51]: ['4', '1', '.', '2', '5', ' ']
In [52]: nonalfa = [ c for c in set(t) if not c.isalpha() ]
In [53]: for c in nonalfa:
...: t.replace(c, ' ')
...:
In [54]: t
Out[54]: 'òeirtup ouh45125p1iuht òkjhgòkjwrhfq...222'
In [55]: for c in nonalfa:
...: t = t.replace(c, ' ')
...:
In [56]: t
Out[56]: 'òeirtup ouh p iuht òkjhgòkjwrhfq '
In [57]: t.split()
Out[57]: ['òeirtup', 'ouh', 'p', 'iuht', 'òkjhgòkjwrhfq']
In [58]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni/lezione7.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni')
In [59]: parole = estrai_parole('files/alice_it.txt', 'latin')
In [60]: parole[:20]
Out[60]: ['Charles', 'Lutwidge', 'Dodgson', 'Alice', 'nel', 'paese', 'delle', 'meraviglie', 'Questo', 'e', 'book', 'è', 'stato', 'realizzato', 'anche', 'grazie', 'al', 'sostegno', 'di', 'E']
In [61]: with open("paperino.txt", mode='w', encoding='utf8') as F:
...: F.write("Paperino andò al mare\n")
...: for i in range(10):
...: print(i, file=F)
...:
In [62]: open("paperino.txt").read()
Out[62]: 'Paperino andò al mare\n0\n1\n2\n3\n4\n5\n6\n7\n8\n9\n'
In [63]: print(open("paperino.txt").read())
Paperino andò al mare
0
1
2
3
4
5
6
7
8
9
In [64]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni/lezione7.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni')
In [65]: runfile('/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni/lezione7.py', wdir='/home/andrea/Documents/Uni/Didattica/Prog1/2019-20/Lezioni')
In [66]: P = estrai_parole('files/alice_it.txt', enc='latin')
In [67]: len(P)
Out[67]: 22994
In [68]: D = conta_parole(P)
In [69]: D1 = conta_parole_efficiente(P)
In [70]: %time D = conta_parole(P)
CPU times: user 1.65 s, sys: 0 ns, total: 1.65 s
Wall time: 1.66 s
In [71]: %time D1 = conta_parole_efficiente(P)
CPU times: user 3.08 ms, sys: 0 ns, total: 3.08 ms
Wall time: 3.09 ms
In [72]: ls files/
alice_it.txt frankenstein.txt log.log prince.txt testo2.txt words.py
alice.txt holmes.txt logo.png results.txt testo.txt
In [73]: with open("files/logo.png", mode='rb') as F:
...: immagine = F.read()
...:
In [74]: type(immagine)
Out[74]: <class 'bytes'>
In [75]: type("skhdgkah")
Out[75]: <class 'str'>
In [76]: print(immagine[:10])
b'\x89PNG\r\n\x1a\n\x00\x00'
In [77]: import IPython.display as ipd
In [78]: ipd(immagine)
Traceback (most recent call last):
File "<ipython-input-78-36f7ecfb520e>", line 1, in <module>
ipd(immagine)
TypeError: 'module' object is not callable
In [79]:
In [79]: ipd.Image(immagine)
Out[79]:
In [80]: ipd.Image("files/logo.png")
Out[80]:
In [81]: