Skip to content

Commit

Permalink
Merge pull request #8 from VoxeJ/main
Browse files Browse the repository at this point in the history
Автоматизация загрузки данных
  • Loading branch information
kdavjd authored Jul 12, 2024
2 parents 00b0413 + 4cd0577 commit 7754003
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/gui/main_tab/load_file_button.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import csv
import os

from PyQt6.QtCore import QSize, pyqtSignal
Expand Down Expand Up @@ -39,8 +40,8 @@ def __init__(self, file_path, parent=None):
self.file_path_edit = QLineEdit(file_path)
self.columns_names_edit = QLineEdit()
self.columns_names_edit.setPlaceholderText("temperature, 3, 5, 10")
self.delimiter_edit = QLineEdit(',')
self.skip_rows_edit = QLineEdit('0')
self.delimiter_edit = QLineEdit("")
self.skip_rows_edit = QLineEdit("")

layout = QVBoxLayout()

Expand All @@ -64,6 +65,12 @@ def __init__(self, file_path, parent=None):

self.setLayout(layout)

self.auto_update_delimeter()
self.auto_update_skip_rows()

self.file_path_edit.textChanged.connect(self.auto_update_delimeter)
self.delimiter_edit.textChanged.connect(self.auto_update_skip_rows)

def file_path(self):
return self.file_path_edit.text()

Expand All @@ -75,3 +82,38 @@ def delimiter(self):

def skip_rows(self):
return int(self.skip_rows_edit.text())

def auto_update_delimeter(self):
if not os.path.isfile(self.file_path()):
logger.error("Некорректный путь к файлу")
return
try:
with open(self.file_path(), 'r') as file:
data = file.read(1024)
sniffer = csv.Sniffer()
dialect = sniffer.sniff(data)
self.delimiter_edit.setText(dialect.delimiter)
logger.debug(f"Определён разделитель: \"{dialect.delimiter}\"")
except csv.Error:
logger.error("Не удалось определить разделить")

def is_data_line(self, line, delimeter):
try:
parts = line.split(delimeter)
float(parts[0].replace(',', '.'))
float(parts[1].replace(',', '.'))
return True
except ValueError:
return False

def auto_update_skip_rows(self):
if not os.path.isfile(self.file_path()):
logger.error("Некорректный путь к файлу")
return
with open(self.file_path(), 'r') as file:
for line_number, line in enumerate(file):
if self.is_data_line(line, self.delimiter()):
self.skip_rows_edit.setText(str(line_number))
logger.debug(f"Определено количество пропускаемых строк: {line_number}")
return
logger.error("Не удалось определить количество пропускаемых строк")

0 comments on commit 7754003

Please sign in to comment.