随笔分类 -  python

摘要:我采用的是MySQLdb操作的MYSQL数据库。先来一个简单的例子吧:?12345678910importMySQLdbtry:conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='test',port=3306)cur=conn.cursor()cur.execute('select*fromuser')cur.close()conn.close()exceptMySQLdb.Error,e:print"MysqlErro 阅读全文
posted @ 2013-09-10 09:33 gxcherie 阅读(377) 评论(0) 推荐(0) 编辑
摘要:importtimeprinttime.time()输出的结果是:1279578704.6725271但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来处理:time.localtime(time.time())用time.localtime()方法,作用是格式化时间戳为本地的时间。输出的结果是:time.struct_time(tm_year=2010,tm_mon=7,tm_mday=19,tm_hour=22,tm_min=33,tm_sec=39,tm_wday=0,tm_yday=200,tm_isdst=0)现在看起来更有希望格式成我们想要的时. 阅读全文
posted @ 2013-09-10 09:32 gxcherie 阅读(499) 评论(0) 推荐(0) 编辑
摘要:一、文件操作1、判断一个目录是否存在,若不存在则创建ifnotos.path.isdir(new_path):os.makedirs(new_path)2、新建一个文件f=open("filename",'w')f.close()首先会判断这个文件是否存在,若不存在则新建,。也可以在open和close直接f.write()函数写文件。3、比较两个文件是否相同importfilecmpfilecmp.cmp(r'文件1',r'文件2')如果两个文件相同。会出输出true。否则输出false二、系统级操作1、获取当前运行脚本所在 阅读全文
posted @ 2013-09-10 09:32 gxcherie 阅读(653) 评论(0) 推荐(0) 编辑
摘要:python获取当前系统时间:nowTime=time.localtime()获取当前系统日期:nowDate=datetime.datetime(nowTime[0],nowTime[1],nowTime[2])日期格式转换:baseTime="20121012"#转换成2012-10-12mouth=baseTime[4:6]year=baseTime[0:4]day=baseTime[6:8]Destime=year+"-"+mouth+"-"+day获取当前系统时间,并转换成字符串类型:curTime=time.strftim 阅读全文
posted @ 2013-09-10 09:32 gxcherie 阅读(1199) 评论(0) 推荐(0) 编辑
摘要:python中有三个内建函数:列表,元组和字符串,他们之间的互相转换使用三个函数,str(),tuple()和list(),具体示例如下所示:>>>s="xxxxx">>>list(s)['x','x','x','x','x']>>>tuple(s)('x','x','x','x','x')>>>tuple(list(s))('x' 阅读全文
posted @ 2013-09-09 11:41 gxcherie 阅读(4434) 评论(0) 推荐(1) 编辑
摘要:Python中如果有中文注释则通常会报错:sys:1:DeprecationWarning:Non-ASCIIcharacter'\xe6'infile./backup.pywonline7,butnoencodingdeclared; seehttp://www.python.org/peps/pep-0263.htmlfordetails。这是因为文件中有非ASCII字符,需要在第一行或第二行指定编码声明。修改方法是,必须在第一行或者第二行加上#coding=,应该与vi中的setfileencoding值相对应 阅读全文
posted @ 2013-09-09 10:26 gxcherie 阅读(561) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示