1. Write operation
import xlwt
book = xlwt.Workbook () # create a new workbook
sheet = book.add_sheet ( 'Sheet1') # Create a worksheet
sheet.write (0,0, 'test') # a new data in the first row and first column
= stus [
[ 'ID', 'name', 'Sex "," Age "," addr', 'Grade', 'Phone', 'Gold'],
[314, 'mineral water', 'M', 18 , 'Changping District, Beijing,' 'Capricorn', '18317155663', 14405],
[315, 'mineral water', 'female', 27, 'Shanghai', 'Capricorn', '18317155664', 100],
[5985 , 'mineral water', 'M', 18 'Changping area', 'classes', '18513867663', 100]
]
The loop is added to the list of data excle
method one:
row = 0
for stu in stus:
col = 0
for filed in stu:
sheet.write(row, col, filed)
col += 1
row += 1
Method Two:
for row,stu in enumerate(stus):
for col,filed in enumerate(stu):
sheet.write(row,col,filed)
book.save('student.xls')
2. Read
import xlrd
book = xlrd.open_workbook('student.xls') # 打开excle
sheet = book.sheet_by_index (0) # Get index sheet sheet sheet in the
sheet = book.sheet_by_name ( 'sheet') # Get the name sheet to sheet in the sheet
res = sheet.cell (0, 0) .value # obtaining a first column of the first row of the content
SUMMARY # Get the first row row = sheet.row_values (0)
col = sheet.col_values (0) # obtain the content of the first column
print (sheet.nrows) # View a total of how many rows
print (sheet.ncols) # View a total of how many columns
# Cycle to obtain list of contents
for i in the Range (1, sheet.nrows):
Print (sheet.row_values (i))