'''
Description: numpy数据的保存和提取
Autor: 365JHWZGo
Date: 2021-11-12 18:32:54
LastEditors: 365JHWZGo
LastEditTime: 2021-11-12 20:25:09
'''
import os
import numpy as np
# 超参数
path = 'pytorch/datasets/day_wise_datashrink.csv'
#文末提取
# 打印当前目录的文件
# print('pytorch里的文件:n',os.listdir("."))
# 原始数据
# 打开并以只读的方式查看文件
with open(path, 'r') as f:
print('原始数据n', f.read())
# 从中抽离出需要处理的数据
# 抽取出标题
title = np.loadtxt(path, delimiter=",", dtype=str, max_rows=1)[np.newaxis, :]
# print(title.shape)
# YMD年月日【yyyy-mm-dd => yyyymmdd】
YMD = np.loadtxt(path, delimiter=",", skiprows=1,
max_rows=10, dtype=object, usecols=0)
YMD = [int(''.join(i.split('/'))) for i in YMD]
# print('年月日:n',YMD)
# 其他数据
# 获得列的长度
length = len(np.loadtxt(path, delimiter=",",
skiprows=1, max_rows=1, dtype=object))
# 获得col下标
col = [i for i in range(length)]
# 对数据进行处理
otherInt1 = np.loadtxt(path, delimiter=",", skiprows=1,
max_rows=10, dtype=int, usecols=col[1:-4])
otherInt2 = np.loadtxt(path, delimiter=",", skiprows=1,
max_rows=10, dtype=int, usecols=col[-1])
otherFloat1 = np.loadtxt(path, delimiter=",", skiprows=1,
max_rows=10, dtype=float, usecols=col[-4:-1])
# print(otherFloat1)
# 整合数据
# 方法1:比较繁琐,先创建一个shape的数组,之后对其进行替换
# after = np.linspace(1,length*10,length*10).reshape(10,length)
# 方法2:使用concatenate对其进行合并
YMD = np.array(YMD)[:, np.newaxis]
# print(YMD)
after = np.concatenate(
[YMD, otherInt1, otherFloat1, otherInt2[:, np.newaxis]], axis=1, dtype=object)
after = np.concatenate([title, after], axis=0, dtype=object)
print('处理之后的数据:n', after)
# 变成更好看的格式
print('展示数据'.center(40, '-'))
for i in range(len(after[0])):
print(after[0][i], end='|')
print()
for i in range(1, len(after)):
for j in range(len(after[i])):
print(str(after[i][j]), end='t')
print()
在本文中使用到的数据day_wise_datashrink.csv
欢迎下载使用!



