Python

Python 創(chuàng)建文件

#寫入數(shù)據(jù)
file=open('a.txt','w',encoding='utf-8')
file.write("哈哈哈哈你好")
file.close()
#讀取文件內(nèi)容
file=open('a.txt','r',encoding='utf=8')
print(file.read())
file.close()

Python 對數(shù)據(jù)庫進行操作--增刪改查

#導包
import pymysql

class Shu():
#有參構造
    def __init__(self,id,name,price,author,publish):
        self.id=id
        self.name = name
        self.price = price
        self.author = author
        self.publish=publish
#無參構造
    def __init__(self):
        pass


con=pymysql.connect(host='127.0.0.1',user='root',password='123456',database='books')
#用于輸送SQL語句執(zhí)行對數(shù)據(jù)庫的操作
cur=con.cursor()

# 增
# result=cur.execute("insert into book values(11,'哈哈哈','33','馮麗娟','北京出版社')")
# if result > 0:
#     print("插入成功")
# else:
#     print("插入失敗")

# 刪
# result=cur.execute("delete from book where id=4")
# print(result)

# 改
# result=cur.execute("update book set price=10 where price=20")
#
# if result > 0:
#     print("修改成功")
# else:
#     print("修改失敗")

# 查
result=cur.execute("select * from book")
string=cur.fetchall()[0]#查詢?nèi)?
shu =Shu()
shu.id=string[0]
shu.name=string[1]
shu.price=string[2]
shu.author=string[3]
shu.publish=string[4]
print(shu.id,shu.name,shu.price,shu.author,shu.publish)

Python 對csv進行操作

#python如何導入csv
import csv

with open('book.csv','r',encoding="utf-8") as file:
    #讀取全部
    # vv=csv.reader(file)
    # for i in vv:
    #     print(i)

    #讀取一列
    vv = csv.reader(file)
    for i in vv:
        print(i[1])   

Python 對Excle進行操作

import xlrd#導包

data=xlrd.open_workbook('book.xls')#將表 book.xls到處在桌面,xlrd中的open_workbook-取excel表格內(nèi)容
print(data)#<xlrd.book.Book object at 0x03783670>
sheet=data.sheets()[0]
hang=sheet.nrows#行數(shù)
print(hang)

lie=sheet.ncols#列數(shù)
print(lie)

#輸出表格中所有橫豎列的內(nèi)容
for i in range(hang):
    for k in range(lie):
        print(sheet.cell(i,k))
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容