Como resolver isso com regex?
O arquivo
assets/grades.txt
contém uma lista de pessoas - uma por linha - contendo suas notas. Crie uma regex para gerar uma lista dos alunos que receberam nota B.
Arquivo assets/grades.txt
:
Ronald Mayr: A
Bell Kassulke: B
Jacqueline Rupp: A
Alexander Zeller: C
Valentina Denk: C
Simon Loidl: B
Elias Jovanovic: B
Stefanie Weninger: A
Fabian Peer: C
Hakim Botros: B
Emilie Lorentsen: B
Herman Karlsen: C
Nathalie Delacruz: C
Casey Hartman: C
Lily Walker : A
Gerard Wang: C
Tony Mcdowell: C
Jake Wood: B
Fatemeh Akhtar: B
Kim Weston: B
Nicholas Beatty: A
Kirsten Williams: C
Vaishali Surana: C
Coby Mccormack: C
Yasmin Dar: B
Romy Donnelly: A
Viswamitra Upandhye: B
Kendrick Hilpert: A
Killian Kaufman: B
Elwood Page: B
Mukti Patel: A
Emily Lesch: C
Elodie Booker: B
Jedd Kim: A
Annabel Davies: A
Adnan Chen: B
Jonathan Berg: C
Hank Spinka: B
Agnes Schneider: C
Kimberly Green: A
Lola-Rose Coates: C
Rose Christiansen: C
Shirley Hintz: C
Hannah Bayer: B
Minha tentativa:
import re
def grades():
with open ("assets/grades.txt", "r") as file:
grades = file.read()
#print(grades)
# YOUR CODE HERE
B_entities = re.finditer("(?P<name>[\w ]*):\s(?P<grade>B$)", grades)
counter = 0
for item in B_entities:
print(item.groupdict(['name']))
counter += 1
print(counter)
return B_entities
grades()