写在开头,基本知识
1. python代码,三个''' '''是换行;""" """可以表示多行注释
2.replace('b','%') 将字符串的b换为%
3.字符串格式化,向一个字符串中插入内容
'含有%s的字符串'%'要插入的字符串', 将%s替换
'含有{}的字符串'.format('要插入的字符串'), 将{}替换
4.截取数据 a = "abcdefgsfdg" print(a[4:7]) 左含右不含
5.数据结构,以某些特定的形式存储数据
列表:[a,b,c,d] 字典:{a:b,c:d}
元组:(a,b,c,d) 集合{a,b,c,d}
6.ctrl+alt+/ 格式化代码
7.在列表中可以进行嵌套[a,b,[c,h,k]],写多个[]就可以了
8.列表的增删
列表.append(增加的内容) 列表.remove(删除的内容)
9.python的判断,电脑也会思考喔
if 判断条件: 执行代码
else: 执行代码
10/python的循环
for循环
for i in a_list:
print(i)
for i in range(1,11):
print(i)
11.函数
内建函数:type() int() str() len() round() input()
自己创建的函数:def 函数名(参数): 函数内容
函数(参数1,参数2,参数3);
函数(参数1=传入1,参数2=传入2,参数3=传入3)
函数(传入1,传入2,参数3=传入3)
尽量使用前两种调用的方法
12.读excel文件:
第三方库的安装 在cmd中 pip install xlrd
#打开工作簿
xlsx = xlrd.open_workbook('d:/作业.xls')
#==============读数据============ #找到sheet,两种方法 table = xlsx.sheet_by_index(0) #table2 = xlsx.sheet_by_iname('作业') #根据行列获取单元格的值 #print(table.cell_value(1,0)) print(table.cell(3,0).value) print(table.row(1)[0].value)
13.写excel文件:
第三方库的安装 在cmd中 pip install xlwt
import xlwt
#创建新的工作簿 new_workbook = xlwt.Workbook()
#添加sheet work_sheet = new_workbook.add_sheet('目录')
#往单元格中写内容 work_sheet.write(0,0,'test') new_workbook.save('d:/test.xls')
14.往文件中添加有格式的数据,第三方库xlutils,一种excel模板
1.复制模板 xlrd.open_workbook('模板位置',formatting_info=True)
2.设置格式 xlwt.XFStyle()
样式:font borders alignment
3.写入内容 write(行,列,内容,样式名)
4.保存 save('保存位置')
import xlwt,xlrd from xlutils.copy import copy xlsx = xlrd.open_workbook('d:/作业.xls') table = xlsx.sheet_by_name("成绩") all_data = [] for n in range(1,table.nrows): name = table.cell(n,0).value lilun = table.cell(n,1).value jineng = table.cell(n,2).value data = {'name':name,'lilun':lilun,'jineng':jineng} all_data.append(data) a_lilun = [] a_total_score = [] b_lilun = [] b_total_score = [] c_lilun = [] c_total_score = [] d_lilun = [] d_total_score = [] for i in all_data: if i['name'] == '李晓奎': a_lilun.append(i['name']) a_total_score.append(i['lilun']+i['jineng']) if i['name'] == '李金隆': b_lilun.append(i['name']) b_total_score.append(i['lilun']+i['jineng']) if i['name'] == '丰茂升': c_lilun.append(i['name']) c_total_score.append(i['lilun']+i['jineng']) if i['name'] == '欧阳荣晟': d_lilun.append(i['name']) d_total_score.append(i['lilun'] + i['jineng']) tem_excel = xlrd.open_workbook('d:/作业.xls',formatting_info=True) tem_sheet = tem_excel.sheet_by_index(0) new_excel = copy(tem_excel) new_sheet = new_excel.get_sheet(0) style = xlwt.XFStyle() font = xlwt.Font() font.name = '微软雅黑' font.bold = True font.height = 360 style.font = font borders = xlwt.Borders() borders.top = xlwt.Borders.THIN borders.bottom = xlwt.Borders.THIN borders.left = xlwt.Borders.THIN borders.right = xlwt.Borders.THIN style.borders = borders alignment = xlwt.Alignment() alignment.horz = xlwt.Alignment.HORZ_CENTER alignment.vert = xlwt.Alignment.VERT_CENTER style.alignment = alignment new_sheet.write(1,1,len(a_lilun),style) new_sheet.write(2,1,round(sum(a_total_score),2),style) new_sheet.write(3,1,94,style) new_sheet.write(4,1,95,style) new_sheet.write(5,1,91,style) new_sheet.write(6,1,89,style) new_excel.save('d:/成绩统计表.xls')