赞
踩
一共80个数据(只截取前10个数据)
一共80个数据
import pandas as pd
data_pd = pd.read_csv("Sheet1.csv")
# 读取表头元素
head_label = list(Sheet1.columns.values)
# head_label打印结果为
#['序号', '学号', '身高(CM)', '体重(KG)', '鞋码', '发长(CM)', '性别(0/1)']
print(data_pd)
data_pd打印结果
import numpy as np
import pandas as pd
Sheet1 = pd.read_csv("Sheet1.csv")
# 将Sheet1变为矩阵形式存储在Sheet2中
Sheet2 = np.array(Sheet1) # 会自动将首行汉字去除,如果没有可以切片,切除首行数据
# 只提取我们所需要的特征值 ['身高(CM)', '体重(KG)', '鞋码', '发长(CM)', '性别(0/1)']
# 提取Sheet2的所有行与从第二列向后的所有列数据,并转为numpy数组
data_np = np.array(Sheet2[:, 2:])
print(data_np) # 维度 (80, 5)
data_np打印结果
import pandas as pd
data_pd = pd.read_table('Sheet_text.txt', sep=',') # 读入txt文件,分隔符为,
print(data_pd)
data_pd打印结果
import csv
import numpy as np
data_list = []
with open("Sheet1 - 副本.csv", "r") as CSVF:
reader = csv.reader(CSVF)
for line in reader:
data_list.append(line)
data_np = np.array(data_list)
print(data_np) # 维度 (81*7)
data_np打印结果
由于原来的Sheet1数据首行有中文所以读取时不方便读取
建议使用pandas读取,或直接删除首行中文,或将中文改为英文
这里我将中文改为英文
更改后csv文件:
import csv
data_list = []
with open("Sheet_ENG.csv", "r") as CSVF:
reader = csv.reader(CSVF)
for line in reader:
data_list.append(line)
print(data_list) # 将数据存储在列表中
data打印部分结果
import csv
import numpy as np
data_list = []
with open("Sheet1 - 副本.csv", "r") as CSVF:
for line in csv.reader(CSVF):
data_list.append(line)
data_np = np.array(data_list)
print(data_np) # 维度 (81*7)
data_np打印结果
data_list = []
with open('Sheet_text.txt', encoding='utf-8') as file_obj:
# encoding='utf-8'文件编码格式为utf-8,否则文字无法输出
for line in file_obj:
data_list.append(line.rstrip().split(',')) # rstrip()去掉换行符
print(data_list)
import numpy as np
data_list = []
with open('Sheet_text.txt', encoding='utf-8') as file_obj:
for line in file_obj:
data_list.append(line.rstrip().split(','))
data_np = np.array(data_list)
print(data_np) #维度为 (81, 7)
data_np打印结果
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。