from xlutils.copy import copy
向已存在Excel中添加sheet:
#打开需要操作的excel表
wb=xlrd.open_workbook(path)
#复制原有表
newb=copy(wb)
#新增sheet
wbsheet=newb.add_sheet(“sheet名”)
向已存在sheet中添加行
#获取原有excel表中名为‘table'的sheet
tabsheet = newb.get_sheet('table')
#k表示该sheet的最后一行
k=len(tabsheet.rows)
#在原有sheet后面新增数据
tabsheet.write(k,0,data1)
tabsheet.write(k,1,data2)
望采纳!
"""如果文件存在则写入文件,如果文件不存在则创建这个新文件。打开文件时,可指定模式 open(filename, 'w'):
'r':读取模式(只读模式)
'w':写入模式(写入文件,每次运行都会覆盖原来文件)
'a':附加模式(在文件原有的基础上,添加新内容)
'r+':能够读取和写入文件的模式
如果你省略了模式实参,Python将以默认的只读模式打开文件。如果你写入的文件不存在,函数open()将自动创建它。然而,以写入'w'模式打开文件时千万要小心,因为如果指定的文件已经存在,Python将在返回文件对象前清空该文件。
Python只能将字符串写入文本文件。要将数值数据存储到文本文件中,必须先使用函数str()将其转换为字符串格式。"""
filename = 'programming.txt'
#写入文件,每次运行都会覆盖原来文件
with open(filename, 'w') as file_object:
file_object.write("I love programming.\n")
file_object.write("I love cteating new games.\n")
#在文件原有的基础上,添加新内容
with open(filename, 'a') as file_object:
file_object.write("I also love finding meaning in large datasets.\n")
file_object.write("I love creating apps that can run in a browser.\n")
#打开文件filename
with open(filename) as file_object:
for line in file_object:
print(line.rstrip())