目录
# .txt
# .xlsx
# .mat
# .csv
# .yaml
# .npy&.npz
# 读取
path = r'C:\Users\Lenovo\Desktop\a.txt'
with open(path,'r') as f:
data = f.read().splitlines() # 去除换行符号\n
# 写入
result = [['a',1.5,2],['b',1.7,3],['c',9.3,4]]
np.savetxt(path,result,'%s')
# 读取
data = pandas.read_excel(path,sheet_name='P_D',header=None) # header=None对应的是纯数据,无表头,正常有表头
有表头时读取某一列
a = data.iloc[:,0].values # iloc用序号索引列 常用
a = data.loc[:,'num'].values # loc是用str索引列
读取某一行
c = data.iloc[0,:].values # header无论是啥,读取的都是数据的第一行,不会出现表头的那一行
常用:
pp = [data.iloc[i,:].values for i in range(data.shape[0])] data.shape[0]从数据开始,不会算上表头的那一行
# 写入
import pandas
path = r'C:\Users\Lenovo\Desktop\a.xlsx'
data = [['a',15],['b',23.4],['c',45]]
df = pandas.DataFrame(data,columns=['alp','num']) # columns指定每一列的名称
df.to_excel(path,sheet_name='a',index=False) # index=False,就会左紧邻,不会有额外的序号占一列
# 读取
import scipy.io
data = scipy.io.loadmat(path)['A']
# 写入
import scipy.io
result = [[1,2,3],[4.5,6.5,2],[6,4,2.3]]
scipy.io.savemat(path,{'A':result}) # 保存为字典格式:使用时用A索引
import numpy as np
data_path = r'D:\毕业设计\数据采集平台\Time_Sequences\01-23-16-36-47_.csv'
data = np.loadtxt(data_path,dtype=float,delimiter = ',')
# 需要加delimiter = ',' 因为中间有逗号
# 读取
import yaml
with open(path,'r') as f:
data = yaml.load(f,Loader=yaml.FullLoader) # 注意:BaseLoader读出来时str类型,所以一般用FullLoader
# 写入
import yaml
a = {'name':'hjw','year':24,'hobby':'soccer','data':{'a':1.2,'b':3.2}}
with open(path,'w') as f:
yaml.dump(a,stream=f)
# .npy文件就是np.save()之后产生的
# .npz文件是多个numpy保存到一起 '''
a = np.array([1,2,3,4,5])
b = np.array([6,7,8,9,10])
np.savez(r'D:\hjw.npz',n1=a,n2=b) # 修改名称为n1,n2
np.savez_compressed() # 用法一样,只是耗时更长,有了压缩
c = np.load(r'D:\hjw.npz')
print(c.files) # ['n1', 'n2']
print(c['n1']) # [1 2 3 4 5]
print(c['n2']) # [ 6 7 8 9 10]