参考下面的代码,如何使用python将<]中的变量替换为数值?
var LVdemact=[ Home1, Home2, Home3, Home4, Home5]
应该取代&#39; Home&#39;来自Excel文件。
抱歉,我没有太多使用python编程的经验
答案 0 :(得分:2)
听起来最适合使用dictionary
来映射值from: to
。这似乎是你所描述的:
from xlrd import open_workbook
from xlutils.copy import copy
import xlsxwriter
rb = open_workbook("Workbook1.xlsx")
# Create an new Excel file and add a worksheet.
wb = xlsxwriter.Workbook('Updated_Workbook1.xlsx')
ws = wb.add_worksheet()
s_orig = rb.sheet_by_index(0)
LVdemact = {'Home1': 1234, 'Home2': 435, 'Home3': 346, 'Home4': 768, 'Home5': 876}
for row in range(s_orig.nrows):
for col in range(s_orig.ncols):
if s_orig.cell(row,col).value in LVdemact:
# s.write(row, col, LVdemact[item])
ws.write(row, col, LVdemact[s_orig.cell(row,col).value])
else:
ws.write(row, col, s_orig.cell(row,col).value)
wb.close()