在<string>'中需要将字符串作为左操作数,而不是元组

时间:2019-10-16 16:23:04

标签: python python-3.x pandas dataframe

我有一个excel 模板,该模板具有多个包含美国(MTHLY-CA...etc)的工作表

enter image description here

我还有一个数据框,其中包含列StateIDpremiumtest

enter image description here

这个想法是循环遍历工作表名称,并将其与数据框中的StateID列进行比较。如果状态匹配,则将premiumtest值写入模板中的特定单元格。

import openpyxl
# getting workbook
wb = openpyxl.load_workbook(r'\server\user\Python\Template.xlsx')
# looping through worksheets
for sheet in wb.worksheets:
     sheetnames = sheet
     print(sheetnames)


for index, row in df.iterrows():
    if any(x in 'MTHLY-'+ row[0] for x in sheetnames): #[0]is the index for column StateID            
    # Then write premiumtest into cell 1

但是我得到一个错误:

Traceback (most recent call last):

    if any(x in 'MTHLY-'+ row[0] for x in sheetnames):  #
  File "x:\Documents\Python\Treaty Year Report\TreatyYearReport3 - Copy.py", line 68, in <genexpr>
    if any(x in 'MTHLY-'+ row[0] for x in sheetnames):  # 
TypeError: 'in <string>' requires string as left operand, not tuple

print(sheetnames):。

sheetnames

2 个答案:

答案 0 :(得分:2)

获取代码

any(x in 'MTHLY-'+ row[0] for x in sheetnames)

运行正常,您可以将工作表名称获取为

sheetnames = [sheet.title for sheet in wb.worksheets]

基本上可以代替您当前的for循环

for sheet in wb.worksheets:
     sheetnames = sheet
     print(sheetnames)

答案 1 :(得分:2)

您正在将单个工作表对象与字符串进行比较。这就是问题。在下面的代码中,您将遍历每个工作表,然后覆盖sheetnames变量。

for sheet in wb.worksheets:
   sheetnames = sheet
   print(sheetnames)

您希望获取的是worksheet.title的列表,因此,更多类似的内容:

sheetnames = [sheet.title for sheet in wb.worksheets]