from selenium import webdriver import xlrd import xlwt import os.path filepath = "E:\\test.xls" #文件查询,没有就创建 file='test.xls' if os.path.exists(filepath): print("找到文件" + file) else: print("没有文件%s,即将创建" % file) excel = xlwt.Workbook(encoding='utf-8') #创建一个新的工作簿 table = excel.add_sheet('infor', cell_overwrite_ok=True) #添加一个sheet名为info excel.save(filepath) #读取表单数据 index = 0 # index表示表单 try: data = xlrd.open_workbook(filepath) except Exception as e: print("文件读取异常") table = data.sheet_by_index(index) # 读取表单 nrows = table.nrows # 行数 ncols = table.ncols # 列数 colnames = table.row_values(0) # 第一行数据 rolnames = table.col_values(0) # 第一列数据 one = table.cell(0, 1).value # 第一行第二列的值 print("行数%s, 列数%s, 第一行数据%s, 第一列数据%s, 第一行第二列的值%s" % (nrows, ncols, colnames, rolnames, one)) #文件写入 book = xlwt.Workbook(encoding='utf-8') #创建一个新的工作簿 sheet = book.add_sheet('data', cell_overwrite_ok=True) sheet.write(0, 0, 'hello') book.save('E:\\test1.xls')
python文件创建、读取和写入
猜你喜欢
转载自blog.csdn.net/sinat_34209942/article/details/81251999
今日推荐
周排行