Python file、excle、mysql

向文件中写入数据

 file = open('a.txt','w',encoding='utf-8')
 file.write("114514")
 file.close()

读取文件中的数据

file = open('a.txt','r',encoding='utf-8')
a = file.read()
print(a)
file.close()

读取excle表中的数据

#导出excle表后,进入文件左上角文件保存一下
import xlrd

data = xlrd.open_workbook('student.xls')
a = data.sheets()[0]
nrows = a.nrows
ncols = a.ncols

for i in range(1,nrows):
    for j in range(1,ncols):
        print(a.cell(i,j))

mysql操作

需要先下载下方的插件

图片.png
class Person():
    def __init__(self,a,b,c):
        self.a = a
        self.b = b
        self.c = c

import pymysql

con = pymysql.connect(host='127.0.0.1',user='root',password='123456',database='1907test')
sno = con.cursor()

# 查询
sno.execute('select * from course')
a = sno.fetchall()
print(a[0])

b = a[0]
#查询出来把数据填入实体类
person = Person(b[0],b[1],b[2])
print(person.a,person.b,person.c)

sno.close()
con.close()


# 删除
a = sno.execute('delete from student where sno = 108')
if a > 0:
    print('删除成功')
else :
    print('删除失败')
sno.close()
con.close()


# 修改
a = sno.execute('update student set sname = "123" where sno = 105')
con.commit()
if a > 0:
    print('修改成功')
else :
    print('修改失败')
sno.close()
con.close()

你可能感兴趣的:(Python file、excle、mysql)