Olli Graf
10 months ago
3 changed files with 139 additions and 0 deletions
@ -0,0 +1,21 @@ |
|||
from vornamen_reader import read_vornamen |
|||
import re |
|||
|
|||
def print_vornamen_simple(vornamen): |
|||
for vorname in vornamen: |
|||
print(vorname) |
|||
|
|||
def print_vornamen_filtered(vornameni,regex): |
|||
pattern = re.compile(regex) |
|||
for vorname in vornamen: |
|||
r = pattern.search(vorname) |
|||
# print(f"{type(r)}: {r}") |
|||
# s = re.findall(regex,vorname) |
|||
# if s[0] != '': |
|||
if pattern.match(vorname) != None: |
|||
print(vorname) |
|||
|
|||
vornamen = read_vornamen('./Vornamen_Wuppertal_2020.csv') |
|||
|
|||
#print_vornamen_simple(vornamen) |
|||
print_vornamen_filtered(vornamen,'O*') |
@ -0,0 +1,68 @@ |
|||
import re |
|||
import unittest |
|||
import logging |
|||
|
|||
pattern_char_only = re.compile('^[A-Z][a-z]+$') |
|||
pattern_plz = re.compile('^[0-9]{5}$') |
|||
pattern_plz2 = re.compile('^\d{5}$') |
|||
pattern_quote= re.compile(':') |
|||
pattern_tab = re.compile(r'\t') |
|||
pattern_simpson = re.compile('Simpson') |
|||
|
|||
logging.basicConfig( format='%(asctime)-15s [%(levelname)s] %(funcName)s: %(message)s', level=logging.DEBUG) |
|||
class TestRegEgEx(unittest.TestCase): |
|||
def test_match(self): |
|||
m = pattern_char_only.match('Olli') |
|||
|
|||
logging.debug(f'm={m}') |
|||
self.assertIsNotNone(m) |
|||
|
|||
def test_findall(self): |
|||
m = pattern_char_only.findall('Olli') |
|||
logging.debug(f'm={m}') |
|||
self.assertIsNotNone(m) |
|||
|
|||
def test_plz(self): |
|||
patterns = [pattern_plz,pattern_plz2] |
|||
plz = ['42275','5600'] |
|||
|
|||
for pattern in patterns: |
|||
m = pattern.findall(plz[0]) |
|||
logging.debug(f'm={m}') |
|||
self.assertNotEqual('',m[0]) |
|||
m = pattern.findall(plz[1]) |
|||
logging.debug(f'm={m}') |
|||
self.assertEqual([],m) |
|||
|
|||
|
|||
def test_search(self): |
|||
m = pattern_char_only.search('Olli') |
|||
logging.debug(f'm={m}') |
|||
self.assertIsNotNone(m) |
|||
|
|||
m = pattern_char_only.search('123456') |
|||
logging.debug(f'm={m}') |
|||
self.assertIsNone(m) |
|||
|
|||
def test_split(self): |
|||
m = pattern_quote.split('Die:Würde:des:Menschen:ist:unantastbar.') |
|||
logging.debug(f'm={m}') |
|||
self.assertIsNotNone(m) |
|||
|
|||
def test_sub(self): |
|||
m = pattern_tab.sub(' ','Satz\tmit\tTabulator.') |
|||
logging.debug(f'm={m}') |
|||
|
|||
def test_finditer(self): |
|||
m = pattern_simpson.finditer('Homer Simpson, Marge Simpson, Bart Simpson, Lisa Simpson, Maggie Simpson') |
|||
logging.debug(f'm={m}') |
|||
|
|||
for name in m: |
|||
logging.debug(f'Name={name}') |
|||
|
|||
|
|||
|
|||
|
|||
if __name__ == '__main__': |
|||
unittest.main() |
|||
|
@ -0,0 +1,50 @@ |
|||
#encoding: utf-8 |
|||
import re |
|||
|
|||
def read_vornamen(filename): |
|||
dict = {} |
|||
# Der Zähler dient nur dazu, die erste Zeile zu überspringen |
|||
count = 0 |
|||
pattern_csv = re.compile(';') |
|||
|
|||
|
|||
dict['mädchen'] = 0 |
|||
dict['jungs'] = 0 |
|||
dict['divers'] = 0 |
|||
with open(filename,'r') as f: |
|||
for zeile in f: |
|||
if count >0: |
|||
# einzelne Zeile in seine Bestandteile zerlegen |
|||
splitted = pattern_csv.split(zeile.strip()) |
|||
anzahl = splitted[0] |
|||
vorname = splitted[1] |
|||
geschlecht= splitted[2] |
|||
position = splitted[3] |
|||
|
|||
if geschlecht == 'w': |
|||
dict['mädchen'] += 1 |
|||
elif geschlecht == 'm': |
|||
dict['jungs'] += 1 |
|||
else: |
|||
dict['divers'] += 1 |
|||
|
|||
|
|||
|
|||
data = {} |
|||
data['vorname'] = vorname |
|||
data['anzahl'] = int(anzahl) |
|||
data['geschlecht'] = geschlecht |
|||
data['position'] = position |
|||
if vorname not in dict: |
|||
dict[vorname] = data |
|||
else: |
|||
# doppelte Einträge summieren wir auf. |
|||
e = dict[vorname] |
|||
e['anzahl'] = int(anzahl) + (e['anzahl']) |
|||
count +=1 |
|||
|
|||
gesamt = dict['jungs'] + dict['mädchen'] + dict['divers'] |
|||
|
|||
dict['gesamt'] = gesamt |
|||
return dict |
|||
|
Loading…
Reference in new issue