注意:
pip install -U selenium
if (条件表达式):
语句
else:
语句
for i in range(初值,终值+1):
语句
使用xlrd、xlwt模块对excel文件中的数据进行读写。
window+R打开cmd窗口,执行下面命令完成xlrd、xlwt的安装
pip3 install xlrd
pip3 install xlwt
将上文的数据做到excel文件中,作为数据源
import xlrd
#f作为文件对象
f = xlrd.open_workbook(r"e:\dd.xlsx")
#
# sheet页索引号是从0开始的
# table = f.sheet_by_index(0)
# table = f.sheet_by_name("Sheet1")
table = f.sheets()[0]
rows = table.nrows
cols = table.ncols
# print(rows)
# print(table.row_values(0)[0])
for i in range(0,n):
#遍历行数据,然后索引单元格
# print(table.row_values(i)[0])
#直接通过坐标,遍历单元格数据
print(table.cell_value(i,0))
import xlwt
#创建工作簿
f = xlwt.Workbook(encoding="utf-8")
# #创建sheet
sheet1 = f.add_sheet(r'sheet1',cell_overwrite_ok=True)
row0 = ["user00","123456",'123456',"[email protected]"]
#生成第一行
for i in range(0,len(row0)):
sheet1.write(0,i,row0[i]) # 顺序为x行x列写入第x个元素
f.save('e:\dd.xls')
======输出结果======
在e:根目录下创建了一个心的excel工作簿,数据也被写入其中
import csv
#现在要做的就是对csv文件的读操作
with open(r"e:\dd.csv","a",encoding="utf-8") as f:
data = csv.reader(f)
#<_csv.reader object at 0x000001B4E939B518>
# ss
# print(type(data))
for i in data:
print(i)
=======输出结果===========
['username', 'password', 'repassword', 'email']
['user1', '123456', '123456', '[email protected]']
['user2', '123456', '123456', '[email protected]']
['user3', '123456', '123456', '[email protected]']
['user4', '123456', '123456', '[email protected]']
['user5', '123456', '123456', '[email protected]']
['user6', '123456', '123456', '[email protected]']
['user7', '123456', '123456', '[email protected]']
['user8', '123456', '123456', '[email protected]']
['user9', '123456', '123456', '[email protected]']
['user10', '123456', '123456', '[email protected]']
import csv
#现在要做的就是对csv文件的写操作,newline='',主要是避免写入数据之后,有多余的换行
with open(r"e:\dd.csv","a",encoding="utf-8",newline='') as f:
# 往csv文件中写入一行数据
dda = ['user11', '123456', '123456', '[email protected]']
csv_writer = csv.writer(f,dialect="excel")
csv_writer.writerow(dda)
# 往csv文件中写入多行数据
csv_writer.writerows([['user11', '123456', '123456', '[email protected]'],['user12', '123456', '123456', '[email protected]']])