pythonifnotnull
㈠ python问题:
我用的是 Python3:
importos
importsqlite3
__ROOT__="F:/PythonFiels/"
ifnotos.path.exists(__ROOT__):
os.mkdir(__ROOT__)
__LINK__=sqlite3.connect(os.path.join(__ROOT__,'datas.db'))
__CURSOR__=__LINK__.cursor()
defstudents():
students=[]
foriinrange(1,31):
students.append(
{
'学号':i,
'姓名':'李荣浩',
'性别':'男',
'年龄':34,
}
)
returnstudents
definit():
try:
createTable()
except:
pass
forstudentinstudents():
studentPath=os.path.join(__ROOT__,str(student['学号']))
os.mkdir(studentPath)
studentAvatar=os.path.join(studentPath,'{}.jpg'.format(student['学号']))
withopen(studentAvatar,'wb')asf:
pass
studentExcel=os.path.join(studentPath,"{}.csv".format(student['姓名']))
withopen(studentExcel,'w')asf:
f.write("姓名,性别,年龄,照片 ")
f.write("{},{},{},{}".format(
student['姓名'],student['性别'],student['年龄'],'{}.jpg'.format(student['学号'])
))
sql="INSERTINTOstudents(id,name,gender,age,avatar)VALUES('{}','{}','{}','{}','{}')".format(
student['学号'],student['姓名'],student['性别'],student['年龄'],'{}.jpg'.format(student['学号'])
)
if__CURSOR__.execute(sql):
__LINK__.commit()
print('一条数据插入成功 ')
else:
print('一条数据插入失败 ')
returnmenu()
defcreateTable(tablename='students'):
sql="""CREATETABLE{}
(idINT(6)PRIMARYKEYNOTNULL,
nameVARCHAR(10)NOTNULL,
ageVARCAHR(10)NOTNULL,
genderVARCHAR(10)NOTNULL,
avatarVARCHAR(10)NOTNULL)""".format(tablename)
return__CURSOR__.execute(sql)
defread():
print('所有数据列表:')
forvalueinall():
print(value)
returnmenu()
defall():
sql="SELECT*FROMstudents"
r=__CURSOR__.execute(sql)
returnr
definfo():
sql="SELECTcount(*)FROMstudents"
r=__CURSOR__.execute(sql)
number=0
age=0
forvalueinr:
number=value[0]
sql="SELECTavg(age)FROMstudents"
r=__CURSOR__.execute(sql)
forvalueinr:
age=value[0]
print('学生共有{}人,年龄平均数为:{}'.format(number,age))
menu()
defupdate():
forvalueinall():
age=value[2]+1
id=value[0]
sql="UPDATEstudentsSETage='{}'WHEREid={}".format(age,id)
__CURSOR__.execute(sql)
__LINK__.commit()
print('更新完毕!')
returnmenu()defaction(id):
id=str(id)
ifid=='1':
returninit()
elifid=='2':
returnread()
elifid=='3':
returninfo()
elifid=='4':
returnupdate()
else:
print('请对照选项进行选择!')
returnmenu()
defmenu():
id=input("请选择:1.初始化数据2.读取所有数据(请先初始化数据)3.显示学生数量和平均年龄(请先初始化数据)4.每人年龄+1(请先初始化数据)");
action(id)
menu()
㈡ python编程题:
我用的是Python3.6.x ,刚才一个题和这个一样是你问的吗?
importos
importsqlite3
__ROOT__="C:/PythonFiels/"
ifnotos.path.exists(__ROOT__):
os.mkdir(__ROOT__)
__LINK__=sqlite3.connect(os.path.join(__ROOT__,'datas.db'))
__CURSOR__=__LINK__.cursor()
defstudents():
students=[]
foriinrange(1,31):
students.append(
{
'学号':i,
'姓名':'李荣浩',
'性别':'男',
'年龄':34,
}
)
returnstudents
definit():
try:
createTable()
except:
pass
forstudentinstudents():
studentPath=os.path.join(__ROOT__,str(student['学号']))
os.mkdir(studentPath)
studentAvatar=os.path.join(studentPath,'{}.jpg'.format(student['学号']))
withopen(studentAvatar,'wb')asf:
pass
studentExcel=os.path.join(studentPath,"{}.csv".format(student['姓名']))
withopen(studentExcel,'w')asf:
f.write("姓名,性别,年龄,照片 ")
f.write("{},{},{},{}".format(
student['姓名'],student['性别'],student['年龄'],'{}.jpg'.format(student['学号'])
))
sql="INSERTINTOstudents(id,name,gender,age,avatar)VALUES('{}','{}','{}','{}','{}')".format(
student['学号'],student['姓名'],student['性别'],student['年龄'],'{}.jpg'.format(student['学号'])
)
if__CURSOR__.execute(sql):
__LINK__.commit()
print('一条数据插入成功 ')
else:
print('一条数据插入失败 ')
returnmenu()
defcreateTable(tablename='students'):
sql="""CREATETABLE{}
(idINT(6)PRIMARYKEYNOTNULL,
nameVARCHAR(10)NOTNULL,
ageVARCAHR(10)NOTNULL,
genderVARCHAR(10)NOTNULL,
avatarVARCHAR(10)NOTNULL)""".format(tablename)
return__CURSOR__.execute(sql)
defread():
print('所有数据列表:')
forvalueinall():
print(value)
returnmenu()
defall():
sql="SELECT*FROMstudents"
r=__CURSOR__.execute(sql)
returnr
definfo():
sql="SELECTcount(*)FROMstudents"
r=__CURSOR__.execute(sql)
number=0
age=0
forvalueinr:
number=value[0]
sql="SELECTavg(age)FROMstudents"
r=__CURSOR__.execute(sql)
forvalueinr:
age=value[0]
print('学生共有{}人,年龄平均数为:{}'.format(number,age))
menu()
defupdate():
forvalueinall():
age=value[2]+1
id=value[0]
sql="UPDATEstudentsSETage='{}'WHEREid={}".format(age,id)
__CURSOR__.execute(sql)
__LINK__.commit()
print('更新完毕!')
returnmenu()defaction(id):
id=str(id)
ifid=='1':
returninit()
elifid=='2':
returnread()
elifid=='3':
returninfo()
elifid=='4':
returnupdate()
else:
print('请对照选项进行选择!')
returnmenu()
defmenu():
id=input("请选择:1.初始化数据2.读取所有数据(请先初始化数据)3.显示学生数量和平均年龄(请先初始化数据)4.每人年龄+1(请先初始化数据)");
action(id)
menu()
㈢ Python如何实现定时发送qq消息
因为生活中老是忘记各种事情,刚好又在学python,便突发奇想通过python实现提醒任务的功能(尽管TIM有定时功能),也可定时给好友、群、讨论组发送qq消息。其工作流程是:访问数据库提取最近计划——>根据数据内容(提醒时间、提醒对象、提醒内容)设置定时任务——>给特定qq好友发送消息。
1.软件版本:
2.安装依赖环境
pymysql安装:pip install pymysql
qqbot安装:pip install qqbot
3.数据库操作
数据库操作非常简单,跟Java类似,自己去菜鸟教程看一下基础语法就好了。
4.配置qqbot登陆信息
也可以不配置,不配置的话就是每次扫码登陆,但这在Linux系统下不好用,我按说明将配置改成了每次将登陆二维码发到固定qq邮箱。qqbot模块在GitHub上,大家可以去看一下模块说明:qqbot
配置文件默认在用户目录下的.qqbot-tmp/v2.3.conf,linux下类似
相关推荐:《Python视频教程》
5. 自定义功能
6.入口主程序
7.其它
数据库结构:
dropdatabaseif exists info_db;
createdatabaseinfo_db defaultcharactersetutf8;
use info_db;
createtabletyschele_tb(
id int(11) auto_increment primarykey,
worktime timestampnotnull,
namevarchar(10) notnull,
content varchar(100) notnull
)engine=InnoDB auto_increment=1 defaultcharset=utf8;
以上就是循环发送qq消息的代码,以下是项目目录结构,其中一些没有出现的文件是自己测试所用,可以不用关心:
效果图:
㈣ python读取excel表格的问题
importnumpyasnp,pandasaspd
a=pd.read_excel('test.xlsx',index_col=None,header=0)
a=a[a.D.notnull()]
a.B=a.B.fillna(method='pad')
b=a.groupby(['B','E'])
c=b.count()
printc
注意读取时候加上表头,没专表头不行属
㈤ 初学python,if not x和if x is None有啥区别
if not x 等效于 if not bool(x)
if x is None 更复杂,用白话来说,x 就是 None。
而None是一个很特别的对象NoneType,它类似数据版库,或其他语言权的Null(空)。
㈥ python字符串相等if,python判断字符串是否为空用什么方法
reaCode = Noneif ( reaCode == None ): print "is null"else : print "not!"if ( reaCode is None ): print "is null"else : print "not!"reaCode = ''if ( reaCode is '' ): print "is null string"else : print "not!"reaCode = ''if ( reaCode == '' ): print "is null string"else : print "not!"if ( reaCode == None ): print "is null"else : print "not!"if ( reaCode is None ): print "is null"else : print "not!"None是一个空的对象,代表什么都没有。而'',是一个字符串对象,代表一个空的字符串。
㈦ 初学python,if not x和if x is None有啥区别
if
not
x
等效于
if
not
bool(x)
if
x
is
None
更复杂,用白话来说,x
就是
None。
而None是一个很特别的对象NoneType,它类似数据库,或其他语言的Null(空)。