lesson11.ipynb 3.13 KB
Newer Older
cermak's avatar
fix    
cermak committed
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
{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.0-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3",
   "language": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "Soubor má 9 řádků, 67 slov a 403 tisknutelných znaků.\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# Spočítejte, kolik je v souboru řádků, slov a viditelných znaků (tedy bez mezer a konců řádků)\n",
    "\n",
    "with open(\"lipsum.txt\", encoding=\"utf-8\") as f:\n",
    "    radku = slov = znaku = 0\n",
    "    for line in f:\n",
    "        radku += 1\n",
    "        for s in line.split():\n",
    "            slov += 1\n",
    "            znaku += len(s)\n",
    "print(f\"Soubor má {radku} řádků, {slov} slov a {znaku} tisknutelných znaků.\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Zkopíruje soubor do jiného souboru, aby řádky šly v opačném pořadí\n",
    "\n",
    "# 1) hloupý postup, který načte původní soubor do paměti\n",
    "\n",
    "with open(\"lipsum.txt\", encoding=\"utf-8\") as f:\n",
    "    lines = f.readlines()\n",
    "with open(\"lipsum-back.txt\", mode=\"w\", encoding=\"utf-8\") as f:\n",
    "    for line in reversed(lines):\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 2) chytrý postup, který čte soubor odzadu, využívá externí knihovnu \n",
    "# to samé by si šlo naprogramovat pomocí seeku, ale proč když už to někdo udělal.reversed\n",
    "\n",
    "from file_read_backwards import FileReadBackwards\n",
    "\n",
    "with FileReadBackwards(\"lipsum.txt\", encoding=\"utf-8\") as frb:\n",
    "    with open(\"lipsum-back.txt\", mode=\"w\", encoding=\"utf-8\") as f:\n",
    "        for line in frb:\n",
    "            f.write(line + \"\\n\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "0\n0\n57\n0\n0\n59\n0\n0\n0\n55\n"
     ]
    }
   ],
   "source": [
    "# Najděte na každém řádku všechna čísla (oddělená mezerami) a vypíše jejich součet; slova, která nejsou čísly, ignoruje.\n",
    "\n",
    "def safe_int(s):\n",
    "    try:\n",
    "        return int(s)\n",
    "    except ValueError:\n",
    "        return 0\n",
    "\n",
    "with open(\"lipsum.txt\", encoding=\"utf-8\") as f:\n",
    "    for line in f:\n",
    "        print(sum(map(safe_int, line.split())))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}