https://naucse.python.cz/2021/pyladies-praha-podzim2021/sessions/while-functions/
Last active
December 12, 2022 17:02
-
-
Save brabemi/c1f183f8d1ffbb55cdb14a6a6fab536f to your computer and use it in GitHub Desktop.
PyLadies - Praha - podzim 2022 - Lekce 7 – Procvičování & soubory (2. 11. 2022)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# tah pomocí for cyklu | |
def tah(herni_pole, cislo_policka, symbol): | |
"Vrátí herní pole s daným symbolem umístěným na danou pozici" | |
vysledne_pole = '' | |
for pozice in range(len(herni_pole)): | |
if pozice != cislo_policka: | |
# vysledne_pole = vysledne_pole + herni_pole[pozice] | |
vysledne_pole += herni_pole[pozice] | |
else: | |
vysledne_pole += symbol | |
# print(herni_pole[cislo_policka] + symbol + herni_pole[cislo_policka + 1:]) | |
return vysledne_pole | |
# tah pomocí funkce z hodiny | |
def tah(herni_pole, cislo_policka, symbol): | |
return herni_pole[:cislo_policka] + symbol + herni_pole[cislo_policka + 1:] | |
pole = 20*'-' | |
print(pole) | |
pole = tah(pole, 4, "x") | |
print(pole) | |
pole = tah(pole, 0, "x") | |
print(pole) | |
pole = tah(pole, 19, "x") | |
print(pole) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://pastebin.com/pCtTiRzP | |
def vrat_jmeno(data): | |
pozice_mezery = data.index(' ') | |
return data[:pozice_mezery] | |
# uloha | |
# kdo se nudí vymyslí jak udělat první písmeno velké | |
# jana -> Jana | |
def vrat_prijmeni(data): | |
pozice_mezery = data.index(' ') | |
return data[pozice_mezery+1:-5] | |
def vrat_rok(data): | |
return data[-4:] | |
def prvni_velke(slovo): | |
return slovo[0].upper() + slovo[1:].lower() | |
def zpracuj_radek(data): | |
jmeno = prvni_velke(vrat_jmeno(data)) | |
prijmeni = prvni_velke(vrat_prijmeni(data)) | |
inicialy = (jmeno[0] + prijmeni[0]).upper() | |
rok = int(vrat_rok(data)) | |
vek = 2022 - rok | |
return f'{jmeno};{prijmeni};{inicialy};{rok};{vek}' | |
radek = 'jana malá 2003' | |
print(zpracuj_radek(radek)) # -> Jana;Malá;JM;2003;19 | |
mirek = 'mIREK bRABEC 1999' | |
print(zpracuj_radek(mirek)) | |
# vysledek - Jméno;Příjmení;Iniciály;Rok narození;Věk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pokud použiju špatný encoding, tak mi python nebude ukazovat správně některé znaky (typicky háčky a čárky) | |
soubor = open('/home/brabemi/pyladies/07/basnicka.txt', encoding='utf8') | |
obsah = soubor.read() | |
# soubory musím zavírat, jinak by se to operačnímu systému časem nemuselo líbit | |
soubor.close() | |
# pokud použiju with, tak se .close() provede automaticky | |
with open('basnicka.txt', encoding='utf8') as muj_soubor: | |
obsah = muj_soubor.read() | |
# soubor lze načíst celý najednou pomocí .read() nebo ho lze číst řádek po řádku | |
with open('basnicka.txt', encoding='utf8') as muj_soubor: | |
for radek in muj_soubor: | |
# pozor na to, že každý řádek má na konci i odřádkování -> rstrip() | |
print(radek.rstrip(), end='!\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# mode='r' = read - výchozí hodnota pro mode | |
# with open('muj_soubor.txt', encoding='utf8', mode='r') as soubor: | |
# mode='w' = write - zápis, bacha začne psát na začátek souboru a smaže obsah | |
# with open('muj_soubor.txt', encoding='utf8', mode='w') as soubor: | |
# mode='a' = append - zápis, začne zapisovat na konec souboru | |
# with open('muj_soubor.txt', encoding='utf8', mode='a') as soubor: | |
# Pokud mám mode a nebo w, tak soubor nemusí exitovat a python ho vytvoří, jakmile zapíšu | |
with open('muj_soubor2.txt', encoding='utf8', mode='a') as soubor: | |
print('ahoj můj soubore no 2', file=soubor) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://pastebin.com/pCtTiRzP | |
def vrat_jmeno(data): | |
pozice_mezery = data.index(' ') | |
return data[:pozice_mezery] | |
# uloha | |
# kdo se nudí vymyslí jak udělat první písmeno velké | |
# jana -> Jana | |
def vrat_prijmeni(data): | |
pozice_mezery = data.index(' ') | |
return data[pozice_mezery+1:-5] | |
def vrat_rok(data): | |
return data[-4:] | |
def prvni_velke(slovo): | |
return slovo[0].upper() + slovo[1:].lower() | |
def zpracuj_radek(data): | |
jmeno = prvni_velke(vrat_jmeno(data)) | |
prijmeni = prvni_velke(vrat_prijmeni(data)) | |
inicialy = (jmeno[0] + prijmeni[0]).upper() | |
rok = int(vrat_rok(data)) | |
vek = 2022 - rok | |
return f'{jmeno};{prijmeni};{inicialy};{rok};{vek}' | |
with open(file='vstup.txt', encoding='utf8') as vstup: | |
with open(file='vystup.csv', encoding='utf8', mode='w') as vystup: | |
for radek in vstup: | |
print(zpracuj_radek(radek.rstrip()),file=vystup) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Našes staré hodiny | |
bijí čtyři hodiny |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jana malá 2003 | |
PETRA VELKÁ 1999 | |
jINDRA lAMA 1977 | |
KaReL VtIpNý 1982 | |
Petr Normální 1987 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jméno | Příjmení | Iniciály | Rok narození | Věk | |
---|---|---|---|---|---|
Jana | Malá | JM | 2003 | 19 | |
Petra | Velká | PV | 1999 | 23 | |
Jindra | Lama | JL | 1977 | 45 | |
Karel | Vtipný | KV | 1982 | 40 | |
Petr | Normální | PN | 1987 | 35 |
We can make this file beautiful and searchable if this error is corrected: No commas found in this CSV file in line 0.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Jméno;Příjmení;Iniciály;Rok narození;Věk | |
Jana;Malá;JM;2003;19 | |
Petra;Velká;PV;1999;23 | |
Jindra;Lama;JL;1977;45 | |
Karel;Vtipný;KV;1982;40 | |
Petr;Normální;PN;1987;35 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment