[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
Python作为一个强大的数据处理工具,这里简单介绍下python读取写入excel表格数据。
<>一、python写入excel表格
导入xlwt模块
生成一个空的excel表格,命名为my_test.xls
import xlwt # 创建工作簿 f = xlwt.Workbook() # 创建一个sheet sheet1 = f.add_sheet(
'my_test', cell_overwrite_ok=True) # 保存文件 f.save("my_test.xls")
生成一个空的excel表格如下:
点击用wps打开看看内容如下:
空的内容。
现在写入一些数据。
import xlwt # 创建工作簿 f = xlwt.Workbook() # 创建一个sheet sheet = f.add_sheet(
'my_test', cell_overwrite_ok=True) #定义每一列的大小 col1 = sheet.col(0) col1.width =
256 * 10#大小256x10 col2 = sheet.col(1) col2.width = 256 * 20#大小256x20 #
初始化第一行作为列名 row0 = ['姓名', '成绩'] #写入第一行为列名 for i in range(0, len(row0)): sheet.
write(0, i, row0[i])#第0行第i列 #同理剩下的数据按照上面的方式写入就行 data=[['小明',89], ['小李',91], [
'小王',77], ['小胖',69]] for i in range(len(data)): for j in range(len(data[i])):
#因为第0行为列名,所以这里下标i+1 sheet.write(i+1,j,data[i][j])#第i+1行第j列写入数据data[i][j] # 保存文件
f.save("my_test.xls")
再次用wps打开表格如下
注意看表格数据和表格每列的大小。
写入完成。
<>二、python读取excel表格
导入xlrd模块
import xlrd # 打开excel读取文件 data = xlrd.open_workbook('my_test.xls') #
根据sheet下标选择读取内容 sheet = data.sheet_by_index(0)#一个表格有多个页面,下标从0开始,我们选择第0页面就行 #
获取到表的总行数 nrows = sheet.nrows for i in range(nrows): print(sheet.row_values(i))
打印内容如下