如何将打印值从一张纸写入新创建的一张纸

时间:2018-08-20 11:28:04

标签: python xlrd xlwt

我是Python的新手,所以只对它有所了解,并且非常感谢您的帮助,因为我不知道如何将文件A中的值写入文件B中。

我想:

  1. 从“ mf_mar_2018.xls”(“ saxon”的过滤器)中过滤D列的值
  2. 将找到的值写入一个名为“ saxons.xls”的新文件

我可以获取未过滤的值并将其打印在终端中。

我的脚本在下面:

#import the writer
import xlwt
#open the spreadsheet
workbook = xlwt.Workbook()
#add a sheet named "Club BFA ranking"
worksheet1 = workbook.add_sheet("Club BFA ranking")
#in cell 0,0 (first cell of the first row) write "Ranking"
worksheet1.write(0, 0, "Ranking")
#in cell 0,1 (second cell of the first row) write "Name"
worksheet1.write(0, 1, "Name")
#save and create the spreadsheet file
workbook.save("saxons.xls")

#import the reader
import xlrd
#open the rankings spreadsheet
book = xlrd.open_workbook('mf_mar_2018.xls')
#open the first sheet
first_sheet = book.sheet_by_index(0)
#print the values in the second column of the first sheet
print first_sheet.col_values(1)

1 个答案:

答案 0 :(得分:1)

尝试类似这样的方法。

name = []
rank = []
for i in range(first_sheet.nrows):
    #print(first_sheet.cell_value(i,3)) 
    if('Saxon' in first_sheet.cell_value(i,3)):  
        name.append(first_sheet.cell_value(i,2))
        rank.append(first_sheet.cell_value(i,8))    
        print('a')
for j in range(len(name)):
    worksheet1.write(j+1,0,rank[j])
    worksheet1.write(j+1,1,name[j])


workbook.save("saxons.xls")