pythone函数基础(11)读,写,修改EXCEL
#读EXCEL需要导入xlrd模块---在python控制台pip install xlrd模块
import xlrd
book = xlrd.open_workbook('stu3.xls')
sheet = book.sheet_by_index(0)
# sheet = book.sheet_by_name('sheet1')
# print(sheet.cell(0,0).value)#获取指定单元格的内容
# print(sheet.cell(1,0).value)
# print(sheet.row_values(0)) #获取整行的数据
# print(sheet.row_values(1))
# print(sheet.col_values(0))#获取整列的数据
# print(sheet.col_values(1)) print(sheet.nrows) #行数
print(sheet.ncols) #列数
for row in range(1,sheet.nrows):
print(sheet.row_values(row)) 写EXCEL需要导入 import xlwt -----在python控制台pip install xlwt模块
import xlwt
book = xlwt.Workbook() #新建一个excel
sheet = book.add_sheet('sheet1') #添加一个sheet页 # sheet.write(0,0,'编号')
# sheet.write(0,1,'名字')
# sheet.write(0,2,'性别')
#
# sheet.write(1,0,'1')
# sheet.write(1,1,'马春波')
# sheet.write(1,2,'男') stu_info = [
['编号','姓名','密码','性别','地址'],
[1,'machunbo','sdfsd23sdfsdf2','男','北京'],
[2,'machunbo2','sdfsd23sdfsdf2','男','北京'],
[3,'machunb3','sdfsd23sdfsdf2','男','北京'],
[4,'machunbo4','sdfsd23sdfsdf2','男','北京'],
[5,'machunbo5','sdfsd23sdfsdf2','男','北京'],
[6,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[7,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[8,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[9,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[10,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[11,'machunbo6','sdfsd23sdfsdf2','男','北京'],
]
#6行5列
# row = 0 #行
# for stu in stu_info:
# sheet.write(row,0,stu[0])
# sheet.write(row,1,stu[1])
# sheet.write(row,2,stu[2])
# sheet.write(row,3,stu[3])
# sheet.write(row,4,stu[4])
# row+=1 # row = 0 #行
# for stu in stu_info:
# #stu
# col = 0 # 列
# # [1, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京'],
# for s in stu: #控制列
# sheet.write(row,col,s) #0 3 男
# col+=1
# row+=1 for index,value in enumerate(stu_info):
# index 0
# value ['编号','姓名','密码','性别','地址'] #index 1
#value [1,'machunbo','sdfsd23sdfsdf2','男','北京']
for index2,v2 in enumerate(value):
print(index,index2,v2)
#0 1
#1 machunbo
#2 sdfsd23sdfsdf2
#4 北京
sheet.write(index,index2,v2) book.save('stu3.xls') #wps xls xlsx ,微软的office xls
修改EXCEL,需要import xlutils-----在python控制台pip install xlwt模块
import xlrd
from xlutils import copy #1、先打开原来的excel
#2、复制一份
#3、在复制的excel上修改
#4、保存 book = xlrd.open_workbook('stu3.xls')
new_book = copy.copy(book) #
sheet = new_book.get_sheet(0) #修改excel的时候,得用get_sheet()
sheet.write(0,0,'id')
sheet.write(0,3,'password')
new_book.save('stu3.xls')
pythone函数基础(11)读,写,修改EXCEL的更多相关文章
- Pandas 基础(4) - 读/写 Excel 和 CSV 文件
这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...
- pythone函数基础(12)连接Redis,写数据,读数据,修改数据
需要导入Resdis模块 import redisip = '127.0.0.1'password='123456'r = redis.Redis(host=ip,password=password, ...
- pythone函数基础(8)内置函数学习
内置函数学习# sorted# map# filter# max# sum# round# chr# ord# dir# bool# eval# exec# zipimport mathres = m ...
- pythone函数基础(7)第三方模块学习
一,time模块学习 import time # print(int(time.time()))#时间戳# res = time.strftime('%Y-%m-%d %H:%M:%S')#取当前格式 ...
- pythone函数基础(15)接口开发初识
导入需要的第三方模块 import flaskimport toolsimport json,redisimport random server = flask.Flask(__name__)#新建一 ...
- pythone函数基础(14)发送邮件
导入yagmail模块import yagmailusername='uitestp4p@163.com'password='houyafan123'#生成授权码,qq.163.126都是授权码 ma ...
- pythone函数基础(13)发送网络请求
需要导入urllib模块,request模块发送网络请求有两种方法 第一种方法# from urllib.request import urlopen# from urllib.parse impor ...
- pythone函数基础(10)MD5加密
导入hashlib模块import hashlibs='yulin123456's.encode()#把数字转换成bytes类型m=hashlib.md5(s.encode())print(m.hex ...
- pythone函数基础(9)操作数据库连接
#操作数据库连接import pymysqlconn = pymysql.connect(host='118.24.3.40',user='jxz', password='123456',port=3 ...
随机推荐
- xshell完美开源替代方案(Kitty+MTPuTTY并设置全局字体)
xshell是收费的,过了30天就不能用了.我们应该找一个开源的替代品.说实话windows平台没有什么可选的,就是putty.但是原生的putty不好用,记不住密码,又不支持多标签. Kitty是基 ...
- Android View转为图片保存为本地文件,异步监听回调操作结果;
把手机上的一个View或ViewGroup转为Bitmap,再把Bitmap保存为.png格式的图片: 由于View转Bitmap.和Bitmap转图片都是耗时操作,(生成一个1M的图片大约500ms ...
- Flask即插视图与tornado比较
由于公司使用了Tornado框架和Flask框架,之前一直使用的都是Flask框架,已经对url下面紧跟着视图的写法很固执.刚开始接触Tornado框架,对于其url和视图分开的写法思想上无法转变.今 ...
- 记录添加mvn命令,以及安装jar包到本地仓库
安装版的maven,没有mvn命令,需要先设置环境变量,添加%MAVEN_HOME% =D:\apache-maven-3.3.9path 中添加 %MAVEN_HOME%/bin即可 安装下载好的j ...
- golang 统计uint64 数字二进制存储中1的数量
package main import ( "fmt") // pc[i] is the population count of i.var pc [256]byte fun ...
- 使用ThreadPoolExecutor进行多线程编程
ThreadPoolExecutor有四个构造函数,分别是: 1,ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keep ...
- C++_数字时钟
利用C++语言基础,制作了一个模拟电子时钟的程序. #include<iostream> #include<windows.h> //延时与清屏头文件 using namesp ...
- 深入理解Java虚拟机读书笔记2----垃圾收集器与内存分配策略
二 垃圾收集器与内存分配策略 1 JVM中哪些内存需要回收? JVM垃圾回收主要关注的是Java堆和方法区这两个区域:而程序计数器.虚拟机栈.本地方法栈这3个区域随线程而生,随线程而灭,随着方 ...
- ADB抓取内存命令
1. 在IDE中查看Log信息当程序运行垃圾回收的时候,会打印一条Log信息,其格式如下:D/dalvikvm: <GC_Reason> <Amount_freed>, < ...
- 一条SQL语句执行得很慢的原因有哪些?
说实话,这个问题可以涉及到 MySQL 的很多核心知识,可以扯出一大堆,就像要考你计算机网络的知识时,问你“输入URL回车之后,究竟发生了什么”一样,看看你能说出多少了. 之前腾讯面试的实话,也问到这 ...