Python xlrd/xlwt:比较 2 个 .xls 文件和多次迭代中的值聚合
Python xlrd/xlwt: comparing 2 .xls files and aggregation of values over several iterations
任务:
我有两个电子表格(A 和 B),请参阅#...# 以获取代码中的参考。
首先,我从 A 的第一行开始。
我在第 7 列 (#1#) 中得到一个特定值 V。我还查看了第 15 列,它确定我要在下一步 (#2#) 中添加 V 电子表格 B 的哪一行。
其次,我切换到 B,并查找我在上一步中使用电子表格 A 定义的正确行( #3#)。然后我将 V 添加到该行的第 5 列 (#4#)。之后,我从 A 第二行的下一次迭代开始,依此类推 (#m=m+1#).
我对电子表格中的每一行都这样做 A。这意味着,A 中每一行的每个值 V 都被添加到电子表格 B 中的某个单元格中,但并非 B 中的每一行都必须在其第 5 列中具有来自 A 的值。
Solution/Problem:
我用 Python 3.x 使用 xlrd/xlwt 解决了这个任务。但是,我面临一个问题,即我的脚本仅适用于一次迭代(意味着 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
我找到了解决问题的方法:
我使用列表来存储值。遍历 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)
任务:
我有两个电子表格(A 和 B),请参阅#...# 以获取代码中的参考。
首先,我从 A 的第一行开始。 我在第 7 列 (#1#) 中得到一个特定值 V。我还查看了第 15 列,它确定我要在下一步 (#2#) 中添加 V 电子表格 B 的哪一行。
其次,我切换到 B,并查找我在上一步中使用电子表格 A 定义的正确行( #3#)。然后我将 V 添加到该行的第 5 列 (#4#)。之后,我从 A 第二行的下一次迭代开始,依此类推 (#m=m+1#).
我对电子表格中的每一行都这样做 A。这意味着,A 中每一行的每个值 V 都被添加到电子表格 B 中的某个单元格中,但并非 B 中的每一行都必须在其第 5 列中具有来自 A 的值。
Solution/Problem:
我用 Python 3.x 使用 xlrd/xlwt 解决了这个任务。但是,我面临一个问题,即我的脚本仅适用于一次迭代(意味着 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
我找到了解决问题的方法:
我使用列表来存储值。遍历 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)