ответвлено от main/python-labs
test: done
Этот коммит содержится в:
39
TEMA7/test.py
Обычный файл
39
TEMA7/test.py
Обычный файл
@@ -0,0 +1,39 @@
|
||||
def read_file_to_list(filename):
|
||||
result = []
|
||||
try:
|
||||
f = open(filename, 'r', encoding='utf-8')
|
||||
lines = f.readlines()
|
||||
f.close()
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if line == '':
|
||||
continue
|
||||
elements = line.split(',')
|
||||
row = []
|
||||
for elem in elements:
|
||||
elem = elem.strip()
|
||||
if elem.isdigit() or (elem.startswith('-') and elem[1:].isdigit()):
|
||||
row.append(int(elem))
|
||||
else:
|
||||
try:
|
||||
if '.' in elem:
|
||||
parts = elem.split('.')
|
||||
if len(parts) == 2:
|
||||
if (parts[0].isdigit() or (parts[0].startswith('-') and parts[0][1:].isdigit())) and parts[1].isdigit():
|
||||
row.append(float(elem))
|
||||
else:
|
||||
row.append(elem)
|
||||
else:
|
||||
row.append(elem)
|
||||
else:
|
||||
row.append(elem)
|
||||
except:
|
||||
row.append(elem)
|
||||
|
||||
result.append(row)
|
||||
|
||||
except FileNotFoundError:
|
||||
print(f"Ошибка: Файл '{filename}' не найден!")
|
||||
return []
|
||||
|
||||
return result
|
||||
Ссылка в новой задаче
Block a user