Answer the question
In order to leave comments, you need to log in
How to write all "correct" data and exceptions to logs in Python 3?
In general, the essence is this:
There is a file with a user registration protocol on the site (registrations.txt).
Each row contains information about the person's name, email, and age.
It is necessary to check the data from the file, for each line and as a result of the check, two files must be generated:
class NotNameError(Exception):
def __init__(self, message, input_data = None):
self.message = message
self.input_data = input_data
def __str__(self):
return self.message
class NotEmailError(Exception):
def __init__(self, message, input_data = None):
self.message = message
self.input_data = input_data
def __str__(self):
return self.message
def read_line(line):
print(f'{line}')
name, mail, age = line.split(' ')
age = int(age)
try:
if age < 10 or age > 99:
raise ValueError
except ValueError:
print(f"Недопустимое значение возраста {ValueError}")
try:
if name.isalpha() == False:
raise NotNameError('Ошибка имени')
except NotNameError:
print(f"Недопустимое значение имени {NotNameError}")
try:
if '@' not in mail:
raise NotEmailError('Ошибка электронной почты')
elif '.' not in mail:
raise NotEmailError('Ошибка электронной почты')
except NotEmailError:
print(f"Недопустимое значение для электронной почты {NotEmailError}")
return name, mail, age
text = open('registrations_.txt', 'r', encoding="utf-8")
for line in text:
try:
line_in_text = read_line(line)
except ValueError as val_exc:
if 'unpack' in val_exc.args[0]:
print(f'Пропущено одно или несколько значений {val_exc}')
else:
print(f'Ошибка значения {val_exc}')
text.close()
Answer the question
In order to leave comments, you need to log in
class NotNameError(Exception):
def __init__(self, message, input_data = None):
self.message = message
self.input_data = input_data
def __str__(self):
return self.message
class NotEmailError(Exception):
def __init__(self, message, input_data = None):
self.message = message
self.input_data = input_data
def __str__(self):
return self.message
def read_line(line):
print(f'{line}')
split_text = line.split(' ', 2)
if len(split_text) == 3:
name, mail, age = split_text
else:
raise ValueError('Пропущено одно или несколько значений')
try: age = int(age)
except ValueError: raise ValueError('Ошибка в значении возраста')
if age < 10 or age > 99:
raise ValueError('Недопустимое значение возраста')
if name.isalpha() == False:
raise NotNameError('Ошибка имени (Недопустимое значение имени)')
if '@' not in mail or '.' not in mail:
raise NotEmailError('Ошибка электронной почты (Недопустимое значение для электронной почты)')
return True
good_acc = []
bad_acc = []
text = open('registrations_.txt', 'r', encoding="utf-8")
for line in text.read().splitlines():
try:
if read_line(line):
good_acc.append(line)
except Exception as error:
bad_acc.append(f'{line} | {error}')
with open('registrations_good.log', 'w', encoding='utf-8') as f:
f.write('\n'.join(good_acc))
with open('registrations_bad.log', 'w', encoding='utf-8') as f:
f.write('\n'.join(bad_acc))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question