ответвлено от main/python-labs
40 строки
1.4 KiB
Python
40 строки
1.4 KiB
Python
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
|