Python学习第56天(configpraser模块复习)

  之前在ftp习题实现的过程中,发现自己已经把configpraser模块的知识忘的没有了,然后加上最近上完新课程之后,即将要开始学习网络编程部分前端的知识了,而且早都计划自己开始复习了,所以,最近一段时间可能主要是知识的复习。

一、 configpraser模块

  先看一个文本的模式吧,一般情况下,这就相当于一个比较高级的字典

  [pengpai]

  username = pengpai

  password = 123

  viplevel = 3

  [zhoujielun]

  username = zhoujielun

  password = 45679

  [studentxiaoy]

  username = xiaoy

  password = xiaowenzhi

  viplevel = max

  上面这段文字就是使用configpraser模块弄出来的,下面看一下在代码里面是怎么书写的呢,自己总结了一下,大概是有三种书写方式吧

import configparser
# 如果存在DEFAULT,在遍历其他参数的时候,会默认遍历DEFAULT里面的东西
# 不想被便利出来就不要用
# 书写方法1:
config = configparser.ConfigParser()
config['pengpai'] = {'username': 'pengpai',
                     'password': '123',
                     'viplevel': '3'}
#书写方法2:
config['zhoujielun'] = {}
config['zhoujielun']['username'] = 'zhoujielun'
config['zhoujielun']['password'] = '45679'
#书写方法3:
config['studentxiaoy'] = {}
x = config['studentxiaoy']
x['username'] = 'xiaoy'
x['password'] = 'xiaowenzhi'
x['viplevel'] = 'max'

with open('test_1','w') as f:
    config.write(f)

  然后上次ftp的习题是用这个作为储存用户密码的一个地方

  说一下先大致的看一下他的增删改查功能吧

  1.查

# options 获取某一部分的对应键的信息,并以列表的形式打印出来
config = configparser.ConfigParser()
config.read('test_1')
print(config.options('studentxiaoy'))

# items 将某一部分存在信息读取,将每个键值对以元组的形式反馈出来
config_items = configparser.ConfigParser()
config_items.read('test_1')
print(config_items.items('studentxiaoy'))

# get 获取对应部分键对应的值,如果不存在会继续在DEFAULT里面查找,想到是每个部分都共有的
config1 = configparser.ConfigParser()
config1.read('test_1')
print(config.get('studentxiayo','password'))

  上面三个的输出结果分别是这个样子的:

    ['username', 'password', 'viplevel']
    [('username', 'xiaoy'), ('password', 'xiaowenzhi'), ('viplevel', 'max')]
    xiaowenzhi

  2.增、删、改

# 增加的方法
# add_section 增加部分
config = configparser.ConfigParser()
config.read('test_1')
config.add_section('zhaolei')   # 增加部分信息内容
config.set('zhaolei','username','zhaolei') # 先后顺序是模块名、键值、对应值
config.set('zhaolei','password','556')
with open('test_1', 'w') as f:
    config.write(f)


# 删除
# 删除这个部分
config = configparser.ConfigParser()
config.read('test_1')
config.remove_section('zhaolei')  #删除整个部分
config.remove_option('studentxiaoy','viplevel')  #指定删除某个键值对
config.write(open('test_1','w'))

  然后是自己试着实现之前的账户注册,大致如下:

import configparser

choice_list = '1.注册新账户' 
              '2.直接登录'
class Zhuce:

    def luru(self,test):
        while True:
            f = input('%s>>>'%test)
            d = input('请确认%s>>>'%test)
            if f == d:
                return f

    def zhuce(self):
        username = self.luru('username')
        password = self.luru('password')
        config = configparser.ConfigParser()
        config.read('test_1')
        config.add_section(username)
        config.set(username,'username',username)
        config.set(username,'password',password)
        with open('test_1','w') as f:
            config.write(f)
        print('注册成功')

    def denglu(self):
        while True:
            username = input('username>>>>')
            password = input('password>>>')
            config = configparser.ConfigParser()
            config.read('test_1')
            for i in config.sections():
                if username == config[i]['username'] and password == config[i]['password']:
                    print('welcome!')
                    break
            break
            print('input agin')

    def start(self):
        print(choice_list)
        while True:
            choice = input('>>>')
            if choice == '1':
                self.zhuce()
                break
            elif choice == '2':
                self.denglu()
                break
            else:
                print('选择错误!!!')

if __name__ == '__main__':
    t = Zhuce()
    t.start()

  代码试过了,完全没有问题。

  感觉自己棒棒的,多复习,很开心啊。。。

  

  

原文地址:https://www.cnblogs.com/xiaoyaotx/p/12729414.html