标签:new 使用 成都 for 读取 none 迭代 coding 返回
#学会csv、txt文件的读取与存储,了解pandas函数的参数的使用
#JSON与Excel数据,学会对JSON与Excel数据的读取与存储
import numpy as np
import pandas as pd
t_data={
"name":["唐浩","小王","老王","赵三","李四","王姐"],
"sex":["男","女","男","女","男","女"],
"year":[37,22,15,18,33,25],
"city":["成都","北京","上海","成都","深圳","北京"]
}
#pandas将表格数据读取为DataFrame数据结构,常用的函数有 read_csv、read_table
#创建一个csv文件
# fp=open("temp01.csv","w",newline="",encoding="utf-8")
# writer=csv.writer(fp)
# writer.writerow(("id","name","grade"))
# writer.writerow(("1","唐浩","99"))
# writer.writerow(("2","xiaowang","55"))
# writer.writerow(("3","lisi","65"))
# writer.writerow(("4","zhangshan","77"))
# fp.close()
"""
id,name,grade
1,唐浩,99
2,xiaowang,55
3,lisi,65
4,zhangshan,77
"""
#读取标准csv文件 read_csv()
# df1_read=pd.read_csv("temp01.csv",encoding="utf-8")
# print(df1_read)
#也可以用read_table()读取
# df2_read=pd.read_table("temp01.csv",sep=",",encoding="utf-8")
# print(df2_read)
#可以指定某列做为索引标签
# df3_read=pd.read_csv(open("temp01.csv",encoding="utf-8"),index_col="id")
# print(df3_read)
#层次化索引数据
# df4_read=pd.read_csv(open("temp02.csv",encoding="utf-8"),index_col=[0,"id"])
# print(df4_read)
#指定标题行 #当header=None,系统会多指定出一个标题列表出来,header=0为默认,第一行
#header=None,index_col=[0,"id"]这两个不能共存,在就要报错,
#header=0,index_col=[0,"id"]可以共存
# df5_read=pd.read_csv(open("temp02.csv",encoding="utf-8"),header=0,index_col=[0,"id"])
# print(df5_read)
#name指定列名
# df6_read=pd.read_csv(open("temp01.csv",encoding="utf-8"),index_col="id",header=0,names=["id","name","grade"])
# print(df6_read)
#自定义读取,有时只读一行或几行,总之一句话,只读对我有用的数据 ,skiprows=[0,1,7,-1],列表中写不读的行数
# df7_r=pd.read_csv(open("temp03.csv",encoding="utf-8"),skiprows=[0,1,7,8],header=0)
# print(df7_r)
#只读取的前行数,nrows=n
# df8_r=pd.read_csv(open("temp03.csv",encoding="utf-8"),skiprows=[0,1,7,8],header=0,nrows=3)
# print(df8_r)
#读取选择的列,usecols=["",""]
# df9_r=pd.read_csv(open("temp03.csv",encoding="utf-8"),nrows=2,usecols=["id","name"],skiprows=[0,1,7,8])
# print(df9_r)
#info()查看读取文件的信息
# df10_r=pd.read_csv(open("temp03.csv",encoding="utf-8"),nrows=2,usecols=["id","name"],skiprows=[0,1,7,8])
# print(df10_r.info())
# df11_r=pd.read_csv(open("temp01.csv",encoding="utf-8"))
# # print(df11_r.info())
# #
# # #如果文件信息很多,数据行数很多,我们可以像分页读一样,设个chunksize=n,来逐步读取
# # df12_r=pd.read_csv(open("temp01.csv",encoding="utf-8"),chunksize=2)
# # #返回了一个可以迭代的对像df12_r,可以用for 循环把他的每一个元素都循环打出来,也可有用列表生成式如下面来拿到
# # print([x for x in df12_r][1])
#将以有的DataFrame数据存入csv文件中,用pandas里的to_csv()
df=pd.DataFrame(t_data)
# print(df)
"""
name sex year city
0 唐浩 男 37 成都
1 小王 女 22 北京
2 老王 男 15 上海
3 赵三 女 18 成都
4 李四 男 33 深圳
5 王姐 女 25 北京
"""
df.to_csv("t_data.csv",encoding="utf-8",index=False) #就存进去了,以“,”号分隔
标签:new 使用 成都 for 读取 none 迭代 coding 返回
原文地址:https://www.cnblogs.com/yiyea/p/11441820.html