python 之文本操着
文本操作
f = open("helloworld.txt", 'r')
# 方法1
# 一次性读取文件,如果文件较大则占用内存较大
# print(f.read(120))
# print(f.read())
# 方法2
# print(f.readline())
# print(f.readline())
# print(f.readline())
# 方法3
txt=f.readlines()
print(txt)
print(type(txt))
excel 操作
import xlrd
xls = xlrd.open_workbook("9x9.xlsx")
# sheet_by_index是通过索引值的方式获取sheet,0表示第一个sheet
sheet = xls.sheet_by_index(0)
print(sheet.nrows) # 总行数
print(sheet.ncols) # 总列数
print(sheet.row_values(1)[0]) # 打印表格第二行第一列
****************************************************************************
import xlrd
xls = xlrd.open_workbook("9x9.xlsx")
sheet = xls.sheet_by_index(0)
print(sheet.nrows) # 总行数
print(sheet.ncols) # 总列数
for row in range(sheet.nrows):
for col in range(sheet.ncols):
print(sheet.row_values(row)[col])
***************************************************************
import xlrd
import xlwt
wb=xlwt.Workbook()
sheet=wb.add_sheet(u'测试')
sheet.write(0,0,"数学")
sheet.write(0,1,"英语")
wb.save("automate.xls")
posted on 2020-04-15 17:13 Indian_Mysore 阅读(113) 评论(0) 收藏 举报
浙公网安备 33010602011771号