python常用操作

python常用操作

1.文件读写

1)目录创建
import os
isExists=os.path.exists(path)
if not isExists:
     os.makedirs(path)//可以创建多级目录
2)文件读取
import csv
with  open(filname,"r") as csvfile: 
            reader = csv.reader(csvfile)
            for line in reader:
            	//单行操作
或者
csvfile1 = open(path+"/"+"country.csv","r",encoding="utf-8")
        reader = [each for each in csv.DictReader(csvfile1, delimiter=',')]
        for row in reader:
3)文件写入
fo=open(filename,"w")
        writer=csv.writer(fo)
        for item in jdata:
            writer.writerow([item['DATA_TIME'], item['POSITION_ID'], item['COUNTRY'], item['VISITORS']])
        fo.close()

2自定义日志

def loggingmanage(table):
    # 创建一个logger
    logger = logging.getLogger(table)
    logger.setLevel(logging.DEBUG)
    # 创建一个handler,用于写入日志文件
    # d = os.path.dirname('.')
    # pd=os.path.dirname(d)
    # pdd=os.path.dirname(pd)
    # apd=os.path.abspath(pdd)
    fh = logging.FileHandler("/home/s/log/"+table+".log")
    fh.setLevel(logging.DEBUG)
    # 定义handler的输出格式
    formatter = logging.Formatter( ("================================\n"
                                    "time:%(asctime)s\nlogger:%(name)s\nlevel:%(levelname)s\n"
                                    "file:%(filename)s\nfun:%(funcName)s\nlineno:%(lineno)d\n"
                                    "message:%(message)s" ))
    fh.setFormatter(formatter)
    # 给logger添加handler
    logger.addHandler(fh)
    return logger
logger=loggingmanage("country")

3 mysql数据库读写

1)mysql连接
def getcon():
    conn=None
    cursor1=None
    try:
        # host是选择连接哪的数据库localhost是本地数据库,port是端口号默认3306
        #user是使用的人的身份,root是管理员身份,passwd是密码。db是数据库的名称,charset是编码格式
        conn=pymysql.connect(host="localhost",port=3306,user='ts',passwd='t,db='ts',charset='utf8',local_infile=1)
        # 创建游标对象
        cursor1=conn.cursor()
    except Exception as e:
        logger.error("数据库连接失败")
        logger.error(e)
    return conn,cursor1
2)myql数据库读取
conn=None
cursor1=None
try:
        conn,cursor1=getcon()
        if conn is None or cursor1 is None:
            logger.error("数据库连接获取失败")
            return (False,None,None)
except Exception as e:
        logger.error(e)
try:
        sql="select  。。。。。;"//sql语句
        logger.info(sql)
        cursor1.execute(sql)
        conn.commit()
        (start_date,) = cursor1.fetchone() //获取第一条结果
        ## for (id,) in cursor1: //遍历多条结果
except Exception as e:
        logger.error(e)
        fla=False
finally:
        if not cursor1:
            cursor1.close()
        if not conn:
            conn.close()
3)mysql数据库写入
conn=None
cursor1=None
try:
        conn,cursor1=getcon()
        if conn is None or cursor1 is None:
            logger.error("数据库连接获取失败")
except Exception as e:
        logger.error(e)
try:
            sql = "insert into onetable (date,ountry,province,visitor) values(%s,%s,%s,%s)"
            for line in reader:
            	#//一次插入多条,插入多条用cursor1.execute()
                args.append((date,country,province,int(line[3]))) 
           cursor1.executemany(sql,args)
            # 提交sql语句执行操作
            conn.commit()
except Exception as e:
        logger.error(e)
finally:
        if not cursor1:
            cursor1.close()
        if not conn:
            conn.close()

你可能感兴趣的:(python)