zoukankan      html  css  js  c++  java
  • Python

    Python的第二十天

    一、random模块

     返回1—10之间的一个随机数,不包括10:random.randrange(1,10)

     返回1—10之间的一个随机数,包括10:random.randint(1,10)

     随机选取0-100间的偶数:random.randrange(0,100,2)

     返回一个随机浮点数:random.random()

     返回一个给定数据集合中的随机字符:random.choice()

     从多个字符中选取特定数量的字符:random.sample('abdjihu',3)

     洗牌:

     import random
    a = [0, 1, 2, 3, 4, 5]
    random.shuffle(a)
    print(a)

    [3, 0, 5, 1, 2, 4]

    生成随机字符串:
     import string
    import random
    a = random.sample(string.ascii_uppercase + string.digits, 4)
    print("".join(a))

    56BP

    二、Excel文件处理-openpyxl模块
    1、
    from openpyxl import Workbook
    wb = Workbook() #创建一个Excel文件在内存里
    sheet = wb.active
    print(sheet.title) #打印sheet表名
    sheet.title = "第一个Excel" #改sheet名
    #加数据
    #方式一:数据直接分配到单元格
    sheet["B8"] = "签到"
    sheet["C8"] = "易班"
    #方式二;可以附加行,从第一列开始附加,从最下方空白处(即有数据的下一行),最左处开始(可以输入多行)
    sheet.append(["每天", "三点前", "按时"]) #对应三个单元格
    sheet.append(["记住", "提醒"])
    import datetime
    sheet["A3"] = datetime.datetime.now().strftime("%Y-%m-%d") #python类型会被自动转换

    wb.save("excel_test.xlsx") #保存

    2、遍历文件

    import openpyxl
    wb = openpyxl.load_workbook("excel_test.xlsx") #打开原有文件
    print(wb.sheetnames)
    sheet = wb.get_sheet_by_name("第一个Excel")
    print(sheet["B4"])
    print(sheet["B4"].value)

    #获取指定列的切片数据
    for cell in sheet["A1:A4"]:
    print(cell[0].value)

    #按行遍历
    for row in sheet:
    # print(row)
    for cell in row:
    print(cell.value, end=",")
    print()

    #按行遍历指定行和列
    for row in sheet.iter_rows(min_row=1, max_row=3, max_col=2):
    for cell in row:
    print(cell.value, end=",")
    print()

    #按列遍历
    for col in sheet.columns:
    for cell in col:
    print(cell.value, end=",")
    print()

    #按列遍历指定行和列
    for col in sheet.iter_cols(min_col=1, max_col=2, min_row=1, max_row=3):
    for cell in col:
    print(cell.value, end=",")
    print()

    3、删除
    wb.remove(sheet)

    del wb[sheet]

     

  • 相关阅读:
    CodeForces 347B Fixed Points (水题)
    CodeForces 347A Difference Row (水题)
    CodeForces 346A Alice and Bob (数学最大公约数)
    CodeForces 474C Captain Marmot (数学,旋转,暴力)
    CodeForces 474B Worms (水题,二分)
    CodeForces 474A Keyboard (水题)
    压力测试学习(一)
    算法学习(一)五个常用算法概念了解
    C#语言规范
    异常System.Threading.Thread.AbortInternal
  • 原文地址:https://www.cnblogs.com/sxy2021/p/14413526.html
Copyright © 2011-2022 走看看