首页 > 编程知识 正文

python直接把数据写入文件(python将信息写入文件)

时间:2023-12-18 17:21:45 阅读:317059 作者:WNAA

本文目录一览:

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 %sn' % (row[0],row[1])) //写入文件

cur.close()

t=file_nodeset.readline()

file_nodeset.close()

file_relationship.close()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。