Python全栈开发——hashlib和configparser模块

1.configparser     #与配置文件有关

import configparser

#相当于创建空字典
config=configparser.ConfigParser()

config['man']={
    'name':'lujiacheng',
    'age':'15',
    'sex':'man',
}

config['woman']={}
tog=config['woman']
tog['name']='alex'
tog['age']='18'
tog['sex']='woman'

config['love']={}
config['love']['fourtile']='love'

with open('looger','w') as f:
    config.write(f)        #写入文件
#-----------------
import configparser

config=configparser.ConfigParser()
config.read('looger')            #与配置文件建立关系
print(config.sections())         #读配置文件
   #['man', 'woman', 'love']

print('man' in config)     #判断是否存在
#True

print(config['man']['name'])  #查
#lujiacheng

for each in config['woman']: #遍历,若文件中有[DEFOULT]配置文件,也会打印
    print(each)
# name
# age
# sex
# window
 
#-----------------------------增删改(最后都要有config.write(open('j.txt','w')))
import configparser
config=configparser.ConfigParser()
config.read('looger')

config.add_section('yuan')        #增加一块
config.set('yuan','price','100')   #增加键值对
config.remove_section('woman')     #删除一块
config.remove_option('man','age')           #删除一块中的一部分

config.write(open('j.txt','w'))
 
hashlib模块
import hashlib
#obj=hashlib.md5()  #建立Md5算法,默认可破解
obj=hashlib.md5('lujiacheng'.encode('utf-8'))   #加盐,很难破解
obj.update('hellow'.encode('utf-8'))

print(obj.hexdigest())
 

猜你喜欢

转载自www.cnblogs.com/lujiacheng-Python/p/9727201.html