- 读取不规则的txt数据文件,将每行保存为numpy数组,并将所有numpy数组存入list返回
import numpy as np
def load_irregular_txt(file_name):
numpy_arrays = []
with open(file_name, 'r') as file:
# 逐行读取文件内容
for line in file:
data = np.array(line.split(), dtype=float) # 将字符串分割为浮点数并转换为NumPy数组
numpy_arrays.append(data)
return numpy_arrays
- 检查文件中数据是否存在NaN值,存在返回true,不存在返回false
import numpy as np
def nancheck(filename):
arr = np.loadtxt(filename)
contains_nan = np.isnan(arr)
has_nan = np.any(contains_nan)
return has_nan