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()
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、Python File close() 方法 close() 方法用于关闭一个已打开的文件。关闭后的文件不能再...
    猜猜猜888阅读 601评论 0 0
  • 前言:文件系统是操作系统的重要组成部分,它规定了计算机对文件和目录进行操作处理的各种标准和机制。以此为基础,编程语...
    IIronMan阅读 1,503评论 0 1
  • Python File(文件) 方法 open() 方法 Python open() 方法用于打开一个文件,并返回...
    木易林1阅读 218评论 0 1
  • 今天开始第二篇,前面讲的内容是邮件发送,这里和前面没有任何关系。只是我小项目优化时候,用到了file操作,这里做下...
    小Ping平阅读 630评论 1 2
  • ferrint阅读 1,013评论 0 0

友情链接更多精彩内容