本文目錄一覽:
python將數組寫入excel文件
# 將數據寫入新文件
def data_write(file_path, datas):
f = xlwt.Workbook()
sheet1 = f.add_sheet(u’sheet1′,cell_overwrite_ok=True) #創建sheet
#將數據寫入第 i 行,第 j 列
i = 0
for data in datas:
for j in range(len(data)):
sheet1.write(i,j,data[j])
i = i + 1
f.save(file_path) #保存文件
如何用PYTHON把一組數據寫入一個文件
一、將一組數據追加到文件中
例如:將123追加到文件1.txt的末尾
def init():
with open(‘1.txt’,’r+’) as text:
text.read()
text.write(‘123’)
text.close()
init()
二、將一組數據覆蓋到文件中
將123覆蓋到1.txt文件中,1.txt之前的數據全沒了
def init():
with open(‘1.txt’,’r+’) as text:
text.write(‘123’)
text.close()
init()
python 有沒有把sql結果,直接寫入文件的方法
python把數據庫查詢結果寫入文件的例子如下:
//以只讀方式打開nodeset.txt
file_nodeset=open(“nodeset.txt”,”r”)
file_relationship=open(“follower_followee.txt”,”a”)
t=file_nodeset.readline()
while(”!=t):
cur=conn.cursor()
cur.execute(“select * from follower_followee where followee_id=%s”,t)
rows=cur.fetchall()//從數據庫取出查詢結果的一行
for row in rows: //開始循環處理
if (row[0] in nodeSet):
print(‘haha’)
file_relationship.write(‘%s %s\n’ % (row[0],row[1])) //寫入文件
cur.close()
t=file_nodeset.readline()
file_nodeset.close()
file_relationship.close()
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/241743.html