Python file、excle、mysql

向文件中寫(xiě)入數(shù)據(jù)

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

讀取文件中的數(shù)據(jù)

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

讀取excle表中的數(shù)據(jù)

#導(dǎo)出excle表后,進(jìn)入文件左上角文件保存一下
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()

# 查詢(xún)
sno.execute('select * from course')
a = sno.fetchall()
print(a[0])

b = a[0]
#查詢(xún)出來(lái)把數(shù)據(jù)填入實(shí)體類(lèi)
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()
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1、Python File close() 方法 close() 方法用于關(guān)閉一個(gè)已打開(kāi)的文件。關(guān)閉后的文件不能再...
    猜猜猜888閱讀 493評(píng)論 0 0
  • 前言:文件系統(tǒng)是操作系統(tǒng)的重要組成部分,它規(guī)定了計(jì)算機(jī)對(duì)文件和目錄進(jìn)行操作處理的各種標(biāo)準(zhǔn)和機(jī)制。以此為基礎(chǔ),編程語(yǔ)...
    IIronMan閱讀 1,463評(píng)論 0 1
  • Python File(文件) 方法 open() 方法 Python open() 方法用于打開(kāi)一個(gè)文件,并返回...
    木易林1閱讀 189評(píng)論 0 1
  • 今天開(kāi)始第二篇,前面講的內(nèi)容是郵件發(fā)送,這里和前面沒(méi)有任何關(guān)系。只是我小項(xiàng)目?jī)?yōu)化時(shí)候,用到了file操作,這里做下...
    小Ping平閱讀 599評(píng)論 1 2
  • ferrint閱讀 904評(píng)論 0 0