zoukankan      html  css  js  c++  java
  • 写代码中遇到的小问题

    1、charset 第三方库,用它来检测编码

    charset = chardet.detect(data).get('encoding')  # 判断读取到文件的编码格式
                    if charset:
                        filestr = data.decode(charset)  # 按照编码格式解码
                    else:
                        filestr = data.decode()

    2、datetime.timedelta:表示时间间隔,即两个时间点之间的长度。

    datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)
    import datetime
    day = datetime.datetime.now()
    ddelay = datetime.timedelta(days=1)
    wdelay = datetime.timedelta(weeks = 5)
    ydelay = datetime.timedelta(weeks = 56)
    
    print(day)
    print(day - ddelay)  # 一天前的时间
    print(day + ddelay)  # 一天后的时间
    print(day - wdelay)  # 5 周前
    print(day + wdelay)  # 5 周后
    print(day - ydelay)  # 一年前
    print(day + ydelay)  # 一年后
    yestoday = now - timedelta(days=1) 
    tommorow = now + timedelta(days=1)
    
    next_year = now + timedelta(days = 365)

     3、list嵌套字典的操作

        exist_name = my_db(host='192.168.1.40', user='mysql', password='123456', db='mysql',
                               sql='select username from app_myuser;')
        list_name = []
        #将所有的用户名放在一个list中,进行循环判断
        for i in range(len(exist_name)):
            list_name.append(exist_name[i]['username'])
        if uname in list_name:
            print('您输入的用户名已存在,请重新输入')

    4、使用open打开文件FileNotFoundError: [Errno 2] No such file or directory: 'xxx'

    for cur_path,cur_dir,cur_files in os.walk(path):#读取给定路径的目录,子目录和文件
            for file in cur_files:
                if file.endswith('.txt'):#只判断.txt文件
                    #if os.path.getsize(file):

    with open(file,'rb') as fr
    #这行代码的file必须是在当前路径,要是不在当前路径会报错

    那么,遍历到不在当前目录的文件时会有标题的错,需要加上文件路径

     for cur_path,cur_dir,cur_files in os.walk(path):#读取给定路径的目录,子目录和文件
            for file in cur_files:
                if file.endswith('.txt'):#只判断.txt文件
                    #if os.path.getsize(file):
                    with open(os.path.join(cur_path,file),'rb') as fr:#直接写file的话,只是在当前目录下找
                        data = fr.read()

     5、操作数据库的时候可以直接从库中对比数据,查找数据是否存在,如下

        sql1= 'select username from app_myuser where username="%s";'%uname
        sql2 = 'select username,passwd from app_myuser where username="%s" and passwd ="%s";'%(uname,password)
        if not my_db(host='192.168.1.40', user='mysql', password='123456', db='mysql',sql=sql1):
            print('您输入的用户名不存在,请重新输入')
        else:
            if my_db(host='192.168.1.40', user='mysql', password='123456', db='mysql',sql=sql2):
                print('登录成功')
            else:
                print('您输入的密码不正确')

    注意使用:

    • if 语句后面的函数调用参数太多,最好是先调用一下,得到一个返回值
    • 函数调用里面的sql参数,不要直接那么写,也要单独定义一个变量,再传入
  • 相关阅读:
    tomcat下的web.xml和项目中的web.xml
    tomcat日志详解
    tomcat日志及logback相关日志框架
    关于程序中使用servlet-api.jar和jsp-api.jar与服务器lib包jar包冲突的问题
    windows server数据库备份
    idea为tomcat设置虚拟地址
    GitHub
    MarkDown的用法
    SVN添加用户权限
    第一篇小记
  • 原文地址:https://www.cnblogs.com/blueteer/p/10102505.html
Copyright © 2011-2022 走看看