DataPrepare/utils/HYS_FileReader.py

54 lines
1.4 KiB
Python

from pathlib import Path
from typing import Union
import numpy as np
import pandas as pd
# 尝试导入 Polars
try:
import polars as pl
HAS_POLARS = True
except ImportError:
HAS_POLARS = False
def read_signal_txt(path: Union[str, Path]) -> np.ndarray:
"""
Read a txt file and return the first column as a numpy array.
Args:
path (str | Path): Path to the txt file.
Returns:
np.ndarray: The first column of the txt file as a numpy array.
"""
path = Path(path)
if not path.exists():
raise FileNotFoundError(f"File not found: {path}")
if HAS_POLARS:
df = pl.read_csv(path, has_header=False, infer_schema_length=0)
return df[:, 0].to_numpy()
else:
df = pd.read_csv(path, header=None, dtype=float)
return df.iloc[:, 0].to_numpy()
def read_laebl_csv(path: Union[str, Path]) -> pd.DataFrame:
"""
Read a CSV file and return it as a pandas DataFrame.
Args:
path (str | Path): Path to the CSV file.
Returns:
pd.DataFrame: The content of the CSV file as a pandas DataFrame.
"""
path = Path(path)
if not path.exists():
raise FileNotFoundError(f"File not found: {path}")
# 直接用pandas读取 包含中文 故指定编码
df = pd.read_csv(path, encoding="gbk")
df["Start"] = df["Start"].astype(int)
df["End"] = df["End"].astype(int)
return df