configparser模块,.使用配置文件记住密码,excel模块

configparser
    配置文件解析模块
    配置应用程序的文件
配置信息指的是,程序中有一些数据需要用户自己来指定,不应该固定死,比如qq中开机启动这一个数据
    这就需要配置文件

    对于配置文件而言,我们的程序最常见的就是读取配置文件操作
    当configparser 模块也能修改和创建配置文件 但不常用

import configparser
cfg = configparser.ConfigParser()
cfg.read("my.cfg",encoding="utf-8")

print(cfg.sections()) # 获取所有的分区名字
print(cfg.get("atm","username")) # 获取某个选项的值
print(cfg.get("atm","password")) # 获取某个选项的值

# 所有选项获取的到的都是字符串类型
print(type(cfg.get("atm","password")))

# 提供了 getint  getboolean get float 可以直接帮你转换类
print(cfg.getint("atm","age"))
print(type(cfg.getint("atm","age")))

# 判断是否存在分区 或 选项
print(cfg.has_option("atm","age"))
print(cfg.has_section("atm"))

# 获取某个分区下的所有选项
print(cfg.options("atm"))
# 删除分区 和选项
print(cfg.remove_section("car"))
print(cfg.remove_option("atm","sex"))

# 修改或是添加  如果有就修改 没有则添加
cfg.set("atm","age","30")

# 添加分区
cfg.add_section("test")
cfg.set("test","账号","大山炮")

# 写入文件
with open("my.cfg","wt",encoding="utf-8") as f:
cfg.write(f)

使用配置文件记住密码:

import configparser
import os

def login():
    if exists_usercfg():
        cfg = configparser.ConfigParser()
        cfg.read("user.cfg",encoding="utf-8")
        user = cfg.get("info","username")
        pwd = cfg.get("info","password")
    else:
        user = input("用户名:")
        pwd = input("密码:")

    if user == "李大炮" and pwd == "213":
        print("登录成功!")
        if not exists_usercfg():
            res = input("是否记住密码?y/n")
            if res == "y":
                jzmm(user,pwd)

# 记住密码函数
def jzmm(user,pwd):
    cfg = configparser.ConfigParser()
    cfg.add_section("info")
    cfg.set("info","username",user)
    cfg.set("info","password",pwd)

    with open("user.cfg","wt",encoding="utf-8") as f:
        cfg.write(f)

def exists_usercfg():
    if os.path.exists("user.cfg"):
        return True

login()

xlrd 读取excel表格数据
xlwt 写入数据到excel表格中:

import xlrd
# 读取文件 得到一个工作簿对象
work_book = xlrd.open_workbook("公司机密数据.xlsx")

# 从工作簿中得到一个表
sheet = work_book.sheet_by_index(1)

# print(sheet.row(0)) # 获取第一行的所有数据
# print(sheet.row_len(0)) # 获取第一行有几个单元格
# print(sheet.row_slice(1)) #获取第2行的所有数据
# print(sheet.row_slice(1,2,4)) #获取第2行的 索引为2-4的 不包含4

# 第一个是行索引   第二个是开始列索引 第三个是结束的列索引
# print(sheet.row_types(2,5,6)) # 获取某些单元格的数据类型
# print(sheet.row_types(6,0,4)) # 获取某些单元格的数据类型

# print(sheet.row_values(6,0,4)) # 获取某些单元格的数据值

# print(sheet.nrows) #行数
# print(sheet.ncols) #列数
# print(sheet.name) # 表格名字

# cell 单元格的处理
# print(sheet.cell_type(2,0)) #直接获取类型
# print(sheet.cell(2,0).ctype) # 获取单元格对象
#
# print(sheet.cell_value(2,0)) # 直接取出值
# print(sheet.cell(2,0).value) # 获取单元格对象并获取数据

#
# print(sheet.col_slice(0,3,5)) # # 切片得到的是 一个列表 里面放的单元格
# print(sheet.col_slice(0,3,5)[0]) # 取出第零个单元格
# print(sheet.col_slice(0,3,5)[0].value) #取出第零个单元格 的值
# print(sheet.col_slice(0,3,5)[0].ctype) #取出第零个单元格 的类型
#
# print(sheet.col_types())# 取出一堆类型
# print(sheet.col_values())# 取出一堆数据

# 有几行就循环几次
# for i in  range(sheet.nrows):
    # 通过索引拿到每一行
    # print(sheet.row_slice(i))
    # for cell in sheet.row_slice(i):
    #     # 在每一行中取出每一个单元格
    #     print(cell.value)
    #     print(cell.ctype)

# 将这个表格的数据转成字典类型
# 1.先拿到所有的key
keys = [cell.value for cell in  sheet.row_slice(1)]
print(keys)

persons = []

for i in range(2,sheet.nrows):
    # 有几次循环就有几个人员信息
    # 建立一个新字典
    dic = {}
    rows = sheet.row_slice(i)
    key_index = 0
    for cell in rows:
        if key_index == 3: #说明是日期 需要转换
            dic[keys[key_index]] = str(xlrd.xldate_as_datetime(cell.value,1))
        else:
            dic[keys[key_index]] = cell.value
        key_index += 1
    persons.append(dic)

import xlwt
# 创建工作簿
work = xlwt.Workbook()
# 创建一个表
sheet = work.add_sheet("员工信息数据")

# 写入标题
for k in keys:
    # 行索引  列索引 第三个是要写入的数据
    sheet.write(0,keys.index(k),k)

# 写入数据
for i in persons:
    for k in keys:
        sheet.write(1 + persons.index(i),keys.index(k),label = i[k])
# 保存至文件
work.save("test.xls")

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值