Compare commits
No commits in common. "12530126dcc870c41871237cb51ac6678939a590" and "d48d576891bfa9a98734ae8de1b1bc93e931f2ae" have entirely different histories.
12530126dc
...
d48d576891
53
uke3.py
53
uke3.py
@ -1,53 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Created on Thu Sep 28 08:23:56 2023
|
|
||||||
|
|
||||||
@author: innagumauri
|
|
||||||
"""
|
|
||||||
|
|
||||||
#%% Task 1
|
|
||||||
|
|
||||||
import re
|
|
||||||
|
|
||||||
def student_information(filename):
|
|
||||||
with open(filename, 'r', newline='', encoding='utf-8') as f:
|
|
||||||
lines = f.readlines()
|
|
||||||
stud=[]
|
|
||||||
for line in lines:
|
|
||||||
if "#" in line:
|
|
||||||
continue
|
|
||||||
d=re.findall(r"[^, :\n]+", line)
|
|
||||||
stud.append({"name":d[0], "age":d[1], "phone number":d[2]})
|
|
||||||
return stud
|
|
||||||
|
|
||||||
print(student_information("data.txt"))
|
|
||||||
|
|
||||||
|
|
||||||
#%% Task 2
|
|
||||||
|
|
||||||
import re
|
|
||||||
from pathlib import Path
|
|
||||||
|
|
||||||
def get_imp_file(file):
|
|
||||||
with open(file, 'r', encoding='utf-8') as f:
|
|
||||||
txt = f.read()
|
|
||||||
# re.M gjør at ^ matcher starten av hver linje istedet for bare starten av stringen
|
|
||||||
ptr1 = re.compile(r"^import\s(\w+)", flags=re.M)
|
|
||||||
ptr2 = re.compile(r"^from\s(\w+)", flags=re.M)
|
|
||||||
imports = re.findall(ptr1, txt)
|
|
||||||
imports += re.findall(ptr2, txt)
|
|
||||||
|
|
||||||
# vi filtrerer ut duplikater:
|
|
||||||
res = []
|
|
||||||
[res.append(x) for x in imports if x not in res]
|
|
||||||
return res
|
|
||||||
|
|
||||||
def print_imp_dir(path="./"):
|
|
||||||
p = Path(path)
|
|
||||||
files = list(p.glob('*.py'))
|
|
||||||
for f in files:
|
|
||||||
print(f'{Path.cwd()}+{f}: {get_imp_file(f)}')
|
|
||||||
|
|
||||||
print_imp_dir()
|
|
||||||
# %%
|
|
Loading…
Reference in New Issue
Block a user