2017-09-26 46 views
1

任務:
我有兩個電子表格(一個),見#...#爲引用在代碼中。Python的xlrd/xlwt:比較值的2個.xls文件,並聚集在幾個迭代

首先,我從A的第一行開始。 我在第7列(#1#)中得到一個具體的值V。我也看了第15欄,它確定了我想在下一步(#2#)中添加V的電子表格的哪一行B

其次,我切換到,並尋找哪個我在與電子表格(#3#)上一步驟中所定義的右排。然後,我在列5(#4#)中將V添加到此行。之後,我從第一行開始,在第二行A等等(#m = m + 1#)的第二行。

我爲電子表格中的每一行都做了這個A。這意味着,每一個值的每一行的在V被添加到一些細胞中的電子表格,但不是在每一行必須具有在其列從值5.

解決/問題:
我解決了使用xlrd/xlwt與Python 3.x的這一任務。但是,我面臨一個問題,即我的腳本只能用於一次迭代(意味着A中的一行)。如果我自動運行腳本以進行多次迭代(Python會自動執行A中的每一行腳本),則以前的值會被較新的值覆蓋,從而導致未完成的電子表格B

但是,如果我手動運行它(通過手動更改A行)它的工作原理。我認爲這是因爲,該腳本是手動執行,並以某種方式保存了值,以免它們被覆蓋。因爲,這不是我的情況(在A ...中超過1k行)的選項,我正在尋找一個自動化的解決方案。

有沒有辦法解決這個問題?

import xlwt 
import xlrd 
from xlutils.copy import copy 

grid_file = 'grid_aut_100km.xls'     #spreadsheet B# 
wind_file = 'wind_aut_sample.xls'     #spreadsheet A# 

gridbook = xlrd.open_workbook(grid_file) 
gridsheet = gridbook.sheet_by_index(0) 

windbook_rd = xlrd.open_workbook(wind_file) 
windsheet_rd = windbook_rd.sheet_by_index(0) 

gridbook_wt = copy(gridbook) 
gridsheet_wt=gridbook_wt.get_sheet(0) 

m=1 

def setup(): 
    gridsheet_wt.write(0,5,"sum_capacity") 
    gridbook_wt.save(grid_file) 

def setsumszero(): 
    n=1 
    sum_capacity = int(0) 
    while n <= gridsheet.nrows-1: 
     gridsheet_wt.write(n,5,sum_capacity) 
     n=n+1 
    gridbook_wt.save(grid_file) 

def gridmatch(m,n): 

    id_in_windsheet = windsheet_rd.cell_value(m,15)    #2# 
    n=1 
    id_in_gridsheet = gridsheet.cell_value(n,0) 

    while True: 
     if id_in_windsheet == id_in_gridsheet:     #3# 
      print(str(id_in_windsheet) + " = " + str(id_in_gridsheet)) 
      print("It's a Match in row " + str(n)) 
      break 
     else: 
      n=n+1 
      id_in_gridsheet = gridsheet.cell_value(n,0) 

    sum_capacity_old = gridsheet.cell_value(n,5) 
    print("Old capacity is " + str(sum_capacity_old)) 

    additional_capacity = windsheet_rd.cell_value(m,7)   #1# 
    print("Additional capacity is " + str(additional_capacity)) 

    sum_capacity_new = sum_capacity_old + additional_capacity #4# 
    print("Sum of new capacity is " + str(sum_capacity_new)) 

    gridsheet_wt.write(n,5,sum_capacity_new) 
    print("New capacity is " + str(sum_capacity_new)) 

    gridbook_wt.save(grid_file) 
    print("") 
    print("") 

setup() 
setsumszero() 
m=1 #row in windbook 
n=1 #row in gridbook 

while m <= windsheet_rd.nrows-1: 
    gridmatch(m,n) 
    gridbook_wt.save(grid_file) 
    m=m+1 

回答

0

我得到了一個解決方案來解決我的問題:

我用一個列表來存儲值。在遍歷n行後,我使用xlwt寫下了最終結果。

請在代碼中找到詳細信息。

import xlwt 
import xlrd 
from xlutils.copy import copy 

##make sure that no macros are used and files are not .xlsx! 
grid_file = 'grid_aut_100km.xls' 
wind_file = 'wind_aut_sample.xls' 
##reading gridfile 
gridbook = xlrd.open_workbook(grid_file) 
gridsheet = gridbook.sheet_by_index(0) 
##reading windfile 
windbook_rd = xlrd.open_workbook(wind_file) 
windsheet_rd = windbook_rd.sheet_by_index(0) 
##writing gridfile 
gridbook_wt = copy(gridbook) 
gridsheet_wt=gridbook_wt.get_sheet(0) 

already_used_lists = [] 

def setup(): 
    ##writes header 
    gridsheet_wt.write(0,5,"sum_capacity") 
    gridbook_wt.save(grid_file) 

def gridmatch(m,n): 
    ##list initialisation 
    capacity_list = [] 
    while n <= gridsheet.nrows-1: 
     capacity_list= capacity_list + [0] 
     n=n+1 
     #print(capacity_list) 

    print("List successfully initialised - Nr. of elements in list " + str(len(capacity_list))) 
    print() 

    while m <= windsheet_rd.nrows-1: 
     print("m is " + str(m)) 
     id_in_windsheet = windsheet_rd.cell_value(m,15) 
     print("to check: " + str(id_in_windsheet)) 
     n=1 
     id_in_gridsheet = gridsheet.cell_value(n,0) 

     while True: 
      if id_in_windsheet == id_in_gridsheet: 
       print(str(id_in_windsheet) + " = " + str(id_in_gridsheet)) 
       print("It's a Match in row " + str(n)) 
       break 
      else: 
       n=n+1 
       id_in_gridsheet = gridsheet.cell_value(n,0) 

     print("Btw: m is " + str(m)) 
     print("Btw: n is " + str(n)) 

     additional_capacity = windsheet_rd.cell_value(m,7) 
     print("Additional capacity is " + str(additional_capacity)) 
     capacity_list[n-1] = capacity_list[n-1] + additional_capacity 

     print(capacity_list) 
     print("") 
     print("") 
     m=m+1 

    ##writing capacity to .xls file 
    n=1 
    while n <= gridsheet.nrows-1: 
     total_capacity = capacity_list[n-1] 
     gridsheet_wt.write(n,5,total_capacity) 
     n=n+1 

setup() 

m=1 ##row in windbook 
n=1 ##row in gridbook 

gridmatch(m,n) 

gridbook_wt.save(grid_file)