python 进阶知识

这篇博客深入探讨了Python的进阶知识,包括使用random模块生成随机数,详细介绍了如何操作SQLite数据库,如创建表、插入数据、无条件和有条件查询。此外,还涵盖了对Word文档的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录

1、模块

1.1  random模块

2、操作数据库

2.1  操作SQLite数据库

2.1.1  创建表

2.1.2  给表插入数据

2.1.3  无条件查询表的数据

2.1.4  有条件查询表的数据

3、操作word


1、模块

1.1  random模块

import random

print(random.sample(range(1,12),5))
print(random.choice(range(1,10)))

2、操作数据库

2.1  操作SQLite数据库

2.1.1  创建表

import sqlite3

# 连接数据库school_db.db
con = sqlite3.connect("school_db.db")

# 创建游标
cur = con.cursor()

# 这是准备要执行的sql语句
sql = '''create table t_person(
            pno INTEGER PRIMARY KEY autoincrement,
            pname VARCHAR not NULL,
            age INTEGER )
'''

try:
    # 开始执行sql语句
    cur.execute(sql)
    print("执行成功")

except Exception as e:
    print(e)
    print("执行失败")

# 执行完sql语句之后,一定要记得关闭游标,断开和数据库的连接
finally:
    cur.close()
    con.close()

2.1.2  给表插入数据

import sqlite3

#i_name = input("请输入【姓名】:")
#i_age = input("请输入【年龄】:")
#i_sex = input("请输入性别(1表示男,0表示女):")
#i_birthday = input("请输入【生日】(yyyymmdd):")

con = sqlite3.connect("school_db.db")

cursor = con.cursor()

# 这里的问号只限于SQLite这里使用,Mysql那边就不要再这么写了
sql = "insert into t_person (pname,age) values (?,?)"

try:
    # 传入单条数据用execute
    #cursor.execute(sql,(i_name,i_age))

    # 传入多条数据用executemany,并且多条数据用“[]”给包裹起来
    cursor.executemany(sql,[('小李',23),('小花',34),('小明',28)])

    # 提交,但凡是对表中数据进行改动的操作,都需要commit
    con.commit()
    print("数据提交成功")

except sqlite3.Error as e:
    print("数据插入发生错误:{}".format(e))
    # 如果存在错误就回滚
    con.rollback()

finally:
    if cursor:
        cursor.close()
    if con:
        con.close()

2.1.3  无条件查询表的数据

import sqlite3

con = sqlite3.connect("school_db.db")

cursor = con.cursor()

sql = "select * from t_person"

try:
    cursor.execute(sql)

    # 提取数据集
    result = cursor.fetchall()
    for row in result:
        print(row)

except sqlite3.Error as e:
    print("数据查询发生错误:{}".format(e))

finally:
    if cursor:
        cursor.close()
    if con:
        con.close()

2.1.4  有条件查询表的数据

import sqlite3

istr = input("请输入生日(yyyymmdd):")

con = sqlite3.connect("school_db.db")

cursor = con.cursor()

sql = "select * from student where s_birthday < ?"

try:
    cursor.execute(sql,[istr])
    # 提取数据集
    result = cursor.fetchall()
    for row in result:
        print("学号:{} - 姓名:{} - 性别:{} - 生日:{}".format(row[0], row[1], row[2], row[3]))

except sqlite3.Error as e:
    print("数据查询发生错误:{}".format(e))

finally:
    if cursor:
        cursor.close()
    if con:
        con.close()

3、操作word

import docx,datetime,os

# 打印出当前所在的文件路径
# print(os.getcwd())

# 以输入路径为准建立文件
# os.mkdir("/Users/maoyan/Desktop/cy_python")

# 切换到输入路径下
os.chdir("/Users/maoyan/Desktop/cy_python")

title=input("你的作业名称叫什么:")

# 新建一个word文件
doc=docx.Document()

# 添加段落内容
doc.add_paragraph("第一段落")
doc.add_paragraph("第二段落")

# 获取当前日期时间
now=datetime.datetime.now()
doc.add_paragraph(str(now.year)+"年"+str(now.month)+"月"+str(now.day)+"日")

# 加上文件后缀
title += ".docx"

# 以确定好的文件名,保存文件
doc.save(title)

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值