读取dat文件

def read_dat(filename):
    import numpy as np
    import re
    '''
    读dat文件,
    返回np.array(x),np.array(xerr),np.array(y),np.array(yerr)
    '''
    with open(filename) as f:
        data = f.readlines()
    
    x = []
    y = []
    xerr = []
    yerr = []
    for line in data[3:]:
        res = re.findall(r'^(.*?)\s(.*?)\s(.*?)\s(.*?)\s.*?$',line)[0]
        #    ^(.*?\..*?)\s(.*?\..*?)\s(.*?\..*?)\s(.*?\..*?)\s.*?$
        x.append(float(res[0]))
        xerr.append(float(res[1]))
        y.append(float(res[2]))
        yerr.append(float(res[3]))
    return np.array(x),np.array(xerr),np.array(y),np.array(yerr)

你可能感兴趣的:(python,hxmtsoft,heasoft)