def loadCSVFilePortfolioSignal(csv_path): FileSignals = {} if not os.path.isfile(csv_path): raise __QS_Error__("文件: '%s' 不存在" % csv_path) with open(csv_path) as CSVFile: FirstLine = CSVFile.readline() if len(FirstLine.split(",")) != 3: # 横向排列 CSVDF = readCSV2Pandas(csv_path, detect_file_encoding=True) temp = list(CSVDF.columns) nCol = len(temp) AllSignalDates = [str(int(temp[i])) for i in range(0, nCol, 2)] for i in range(int(nCol / 2)): iDT = CSVDF.columns[i * 2] iSignal = CSVDF.iloc[:, i * 2:i * 2 + 2] iSignal = iSignal[pd.notnull(iSignal.iloc[:, 1])].set_index( [iDT]).iloc[:, 0] FileSignals[AllSignalDates[i]] = iSignal else: # 纵向排列 CSVDF = readCSV2Pandas(csv_path, detect_file_encoding=True, header=0) AllSignalDates = pd.unique(CSVDF.iloc[:, 0]) AllColumns = list(CSVDF.columns) for iDT in AllSignalDates: iSignal = CSVDF.iloc[:, 1:][CSVDF.iloc[:, 0] == iDT] iSignal = iSignal.set_index(AllColumns[1:2]) iSignal = iSignal[AllColumns[2]] FileSignals[str(iDT)] = iSignal return FileSignals
def loadCSVFileTimingSignal(csv_path): FileSignals = {} if not os.path.isfile(csv_path): return FileSignals with open(csv_path) as CSVFile: FirstLine = CSVFile.readline().split(",") if (len(FirstLine)!=2) or (FirstLine[1] is not None):# 横向排列 CSVDF = readCSV2Pandas(csv_path,detect_file_encoding=True,header=0,index_col=None) CSVDF = CSVDF.T else:# 纵向排列 CSVDF = readCSV2Pandas(csv_path,detect_file_encoding=True,header=None,index_col=0) CSVDF = CSVDF.iloc[:,0] for iDT in CSVDF.index: FileSignals[str(iDT)] = CSVDF[iDT] return FileSignals
def importData(self): FilePath, _ = QFileDialog.getOpenFileName(self, "导入 ID", os.getcwd(), "csv (*.csv)") if not FilePath: return 0 try: IDs = readCSV2Pandas(FilePath, detect_file_encoding=True, index_col=None, header=None) except Exception as e: return QMessageBox.critical(self, "错误", "数据读取失败: " + str(e)) self.IDs = IDs.values[:, 0].tolist() return self.populateIDListWidget(self.IDs)
def importData(self): FilePath, _ = QFileDialog.getOpenFileName(self, "导入数据", os.getcwd(), "csv (*.csv)") if not FilePath: return 0 try: Data = readCSV2Pandas(FilePath, detect_file_encoding=True, index_col=None, header=None, parse_dates=True, infer_datetime_format=True) except Exception as e: return QMessageBox.critical(self, "错误", "数据读取失败: " + str(e)) ListWidget = self.sender().parent().parent().parent() if ListWidget is self.DateListWidget: FormatStr = "%Y-%m-%d" elif ListWidget is self.TimeListWidget: FormatStr = "%H:%M:%S" elif ListWidget is self.DateTimeListWidget: FormatStr = "%Y-%m-%d %H:%M:%S.%f" FormatStr, isOk = QInputDialog.getText(self, "时间格式", "请输入文件的时间格式: ", text=FormatStr) if not isOk: return 0 try: if ListWidget is self.DateListWidget: self.Dates = [ dt.datetime.strptime(Data.iloc[i, 0], FormatStr).date() for i in range(Data.shape[0]) ] return self.populateDateListWidget(self.Dates) elif ListWidget is self.TimeListWidget: self.Times = [ dt.datetime.strptime(Data.iloc[i, 0], FormatStr).time() for i in range(Data.shape[0]) ] return self.populateTimeListWidget(self.Times) elif ListWidget is self.DateTimeListWidget: self.DateTimes = [ dt.datetime.strptime(Data.iloc[i, 0], FormatStr) for i in range(Data.shape[0]) ] return self.populateDateTimeListWidget(self.DateTimes) except Exception as e: return QMessageBox.critical(self, "错误", "数据解析失败: " + str(e))