Last active
April 23, 2020 08:47
-
-
Save brabemi/d3d8c7bd9239caa35fa9cb97729e0f09 to your computer and use it in GitHub Desktop.
PyLadies - Praha (NTK) - podzim 2019 - Lekce 6 – Procvičování & soubory
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
def vrat_rok(data): | |
return int(data[-4:]) | |
def spocitej_vek(rok): | |
return 2019 - rok | |
def vrat_cele_jmeno(data): | |
return data[:-5] | |
def vrat_krestni(cele_jmeno): | |
pozice_mezery = cele_jmeno.index(' ') | |
return prvni_velke(cele_jmeno[:pozice_mezery]) | |
def vrat_prijmeni(cele_jmeno): | |
pozice_mezery = cele_jmeno.index(' ') | |
return prvni_velke(cele_jmeno[pozice_mezery + 1:]) | |
def prvni_velke(text): | |
return text[0].upper() + text[1:].lower() | |
def vrat_inicialy(krestni, prijmeni): | |
return krestni[0] + prijmeni[0] | |
def preved_na_csv(data): | |
''' | |
funkce převede data do formátu CSV | |
formát dat: jemno prijmeni rok | |
''' | |
rok = vrat_rok(data) | |
vek = spocitej_vek(rok) | |
cele_jmeno = vrat_cele_jmeno(data) # 'jana malá' | |
krestni = vrat_krestni(cele_jmeno) | |
prijmeni = vrat_prijmeni(cele_jmeno) | |
inicialy = vrat_inicialy(krestni, prijmeni) | |
vysledek = f'{prijmeni};{krestni};{inicialy};{rok};{vek}' | |
return vysledek | |
vstup_radek = 'jana MALÁ 2003' | |
vystup_radek = 'Malá;Jana;JM;2003;16' | |
zpracovany_radek = preved_na_csv(vstup_radek) | |
print(f'{zpracovany_radek} == {vystup_radek} {zpracovany_radek == vystup_radek}') | |
with open('vstup.txt', encoding='utf-8') as vstup: | |
# Pro libreoffice a linux použijte pro vystup.csv kódování utf-8. | |
# Windows a excel si ve výchozím stavu s utf-8 nerozumí. | |
# Na českých windows by mělo fungovat kódování cp1250. | |
# Pokud máte Mac dejte vědět, která verze fungovala vám :) | |
# with open('vystup.csv', mode='w', encoding='cp1250') as vystup: | |
with open('vystup.csv', mode='w', encoding='utf-8') as vystup: | |
# Záhlaví tabulky | |
print('Jméno;Příjmení;Iniciály;Rok narození;Věk', file=vystup) | |
for radek in vstup: | |
radek = radek.rstrip() | |
print(preved_na_csv(radek), 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
jana malá 2003 | |
PETRA VELKÁ 1999 | |
jINDRA lAMA 1977 | |
KaReL VtIpNý 1982 | |
Petr Normální 1987 |
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 | |
Malá;Jana;JM;2003;16 | |
Velká;Petra;PV;1999;20 | |
Lama;Jindra;JL;1977;42 | |
Vtipný;Karel;KV;1982;37 | |
Normální;Petr;PN;1987;32 |
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 | |
---|---|---|---|---|---|
Malá | Jana | JM | 2003 | 16 | |
Velká | Petra | PV | 1999 | 20 | |
Lama | Jindra | JL | 1977 | 42 | |
Vtipný | Karel | KV | 1982 | 37 | |
Normální | Petr | PN | 1987 | 32 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment