将字符串添加到格式化字符串
Adding strings to a formatted string
我正在尝试添加到要进入 excel 的字符串。
sheets_to_use = ["Dermott Wind_Mast 9530", "Dermott Wind_Mast 9531", "Dermott Wind_Mast 9532"]
for num in range(2,14): # 2 through 13
formula_string = "=AVERAGE(" + ",".join(["{}!B{}".format(str(i), str(num)) for i in sheets_to_use]) + ")"
_ = summary_sheet.cell(row=name_row+num, column=3)
_.value = formula_string
_.number_format = '#,####0.0000'
formula_string = "=AVERAGE(" + ",".join(["{}!C{}".format(str(i), str(num)) for i in sheets_to_use]) + ")"
_ = summary_sheet.cell(row=name_row+num, column=4)
_.value = formula_string
_.number_format = '#,####0.0000'
但实际上进入我 sheet 牢房的是:
=AVERAGE(Dermott Wind_Mast '9530'!B2,Dermott Wind_Mast '9531'!B2,Dermott Wind_Mast '9532'!B2)
单引号应该是整个str(i),为什么只有数字有单引号?
不确定我是否理解这个问题,但我也无法重现您的问题。以下是如何使用 f-strings 让您的生活更轻松:
sheets_to_use = ["Dermott Wind_Mast 9530",
"Dermott Wind_Mast 9531",
"Dermott Wind_Mast 9532"]
for num in range(2,14):
# Note the use of single quotes inside this f-string:
sheet_refs = ", ".join(f"'{s}!B{num}'" for s in sheets_to_use)
formula_string = f"=AVERAGE({sheet_refs})"
print(formula_string)
输出:
=AVERAGE('Dermott Wind_Mast 9530!B2', 'Dermott Wind_Mast 9531!B2', 'Dermott Wind_Mast 9532!B2')
=AVERAGE('Dermott Wind_Mast 9530!B3', 'Dermott Wind_Mast 9531!B3', 'Dermott Wind_Mast 9532!B3')
=AVERAGE('Dermott Wind_Mast 9530!B4', 'Dermott Wind_Mast 9531!B4', 'Dermott Wind_Mast 9532!B4')
=AVERAGE('Dermott Wind_Mast 9530!B5', 'Dermott Wind_Mast 9531!B5', 'Dermott Wind_Mast 9532!B5')
=AVERAGE('Dermott Wind_Mast 9530!B6', 'Dermott Wind_Mast 9531!B6', 'Dermott Wind_Mast 9532!B6')
=AVERAGE('Dermott Wind_Mast 9530!B7', 'Dermott Wind_Mast 9531!B7', 'Dermott Wind_Mast 9532!B7')
=AVERAGE('Dermott Wind_Mast 9530!B8', 'Dermott Wind_Mast 9531!B8', 'Dermott Wind_Mast 9532!B8')
=AVERAGE('Dermott Wind_Mast 9530!B9', 'Dermott Wind_Mast 9531!B9', 'Dermott Wind_Mast 9532!B9')
=AVERAGE('Dermott Wind_Mast 9530!B10', 'Dermott Wind_Mast 9531!B10', 'Dermott Wind_Mast 9532!B10')
=AVERAGE('Dermott Wind_Mast 9530!B11', 'Dermott Wind_Mast 9531!B11', 'Dermott Wind_Mast 9532!B11')
=AVERAGE('Dermott Wind_Mast 9530!B12', 'Dermott Wind_Mast 9531!B12', 'Dermott Wind_Mast 9532!B12')
=AVERAGE('Dermott Wind_Mast 9530!B13', 'Dermott Wind_Mast 9531!B13', 'Dermott Wind_Mast 9532!B13')
此外,如评论中所述,使用 _
作为执行操作的变量名是不好的做法。 _
用于向其他读者表示该变量的值不重要,以后不会以任何身份使用。始终为您的变量指定描述性名称!
我正在尝试添加到要进入 excel 的字符串。
sheets_to_use = ["Dermott Wind_Mast 9530", "Dermott Wind_Mast 9531", "Dermott Wind_Mast 9532"]
for num in range(2,14): # 2 through 13
formula_string = "=AVERAGE(" + ",".join(["{}!B{}".format(str(i), str(num)) for i in sheets_to_use]) + ")"
_ = summary_sheet.cell(row=name_row+num, column=3)
_.value = formula_string
_.number_format = '#,####0.0000'
formula_string = "=AVERAGE(" + ",".join(["{}!C{}".format(str(i), str(num)) for i in sheets_to_use]) + ")"
_ = summary_sheet.cell(row=name_row+num, column=4)
_.value = formula_string
_.number_format = '#,####0.0000'
但实际上进入我 sheet 牢房的是:
=AVERAGE(Dermott Wind_Mast '9530'!B2,Dermott Wind_Mast '9531'!B2,Dermott Wind_Mast '9532'!B2)
单引号应该是整个str(i),为什么只有数字有单引号?
不确定我是否理解这个问题,但我也无法重现您的问题。以下是如何使用 f-strings 让您的生活更轻松:
sheets_to_use = ["Dermott Wind_Mast 9530",
"Dermott Wind_Mast 9531",
"Dermott Wind_Mast 9532"]
for num in range(2,14):
# Note the use of single quotes inside this f-string:
sheet_refs = ", ".join(f"'{s}!B{num}'" for s in sheets_to_use)
formula_string = f"=AVERAGE({sheet_refs})"
print(formula_string)
输出:
=AVERAGE('Dermott Wind_Mast 9530!B2', 'Dermott Wind_Mast 9531!B2', 'Dermott Wind_Mast 9532!B2')
=AVERAGE('Dermott Wind_Mast 9530!B3', 'Dermott Wind_Mast 9531!B3', 'Dermott Wind_Mast 9532!B3')
=AVERAGE('Dermott Wind_Mast 9530!B4', 'Dermott Wind_Mast 9531!B4', 'Dermott Wind_Mast 9532!B4')
=AVERAGE('Dermott Wind_Mast 9530!B5', 'Dermott Wind_Mast 9531!B5', 'Dermott Wind_Mast 9532!B5')
=AVERAGE('Dermott Wind_Mast 9530!B6', 'Dermott Wind_Mast 9531!B6', 'Dermott Wind_Mast 9532!B6')
=AVERAGE('Dermott Wind_Mast 9530!B7', 'Dermott Wind_Mast 9531!B7', 'Dermott Wind_Mast 9532!B7')
=AVERAGE('Dermott Wind_Mast 9530!B8', 'Dermott Wind_Mast 9531!B8', 'Dermott Wind_Mast 9532!B8')
=AVERAGE('Dermott Wind_Mast 9530!B9', 'Dermott Wind_Mast 9531!B9', 'Dermott Wind_Mast 9532!B9')
=AVERAGE('Dermott Wind_Mast 9530!B10', 'Dermott Wind_Mast 9531!B10', 'Dermott Wind_Mast 9532!B10')
=AVERAGE('Dermott Wind_Mast 9530!B11', 'Dermott Wind_Mast 9531!B11', 'Dermott Wind_Mast 9532!B11')
=AVERAGE('Dermott Wind_Mast 9530!B12', 'Dermott Wind_Mast 9531!B12', 'Dermott Wind_Mast 9532!B12')
=AVERAGE('Dermott Wind_Mast 9530!B13', 'Dermott Wind_Mast 9531!B13', 'Dermott Wind_Mast 9532!B13')
此外,如评论中所述,使用 _
作为执行操作的变量名是不好的做法。 _
用于向其他读者表示该变量的值不重要,以后不会以任何身份使用。始终为您的变量指定描述性名称!