如何遍历数组?

时间:2020-07-10 19:46:04

标签: python

我刚刚开始使用python,我试图通过一个简单的for循环将一些术语从数组中传递出去,该循环使用基于正则表达式的模式进行搜索。

ethnicities_stemms = ["religion", ["cathol", "protest"]]

for stemm in ethnicities_stemms:
    ethnicity_pattern = re.compile(r'.*'+stemm+'.*')

    ethnicity = expansions.loc[lambda x: x['queryterm'].str.contains(ethnicity_pattern, regex = True)]

    writer = pd.ExcelWriter(stemm+'.xlsx', engine='xlsxwriter')
    ethnicity.to_excel(writer, sheet_name='Sheet1', index=False)
    writer.save()

    print(ethnicity)

我要在这里实现的是,该循环将数组religio放入模式中,并为我提供了包含“天主教”或“抗议”的所有数据,并将它们一起写入一个新的{ {1}}。

每次我尝试运行代码时,都会显示错误消息.xlsx

1 个答案:

答案 0 :(得分:0)

错误是因为列表中的第二项是另一个列表(["cathol", "protest"]),无法将其添加到字符串中(就像您在re.compile(r'.*'+stemm+'.*')中所做的那样)。我猜想您可能正在尝试做这样的事情-创建一个名为“宗教”的Excel工作簿,其工作表名为“天主教”和“抗议”。

ethnicities_stemms = {"religion": ["cathol", "protest"]}

for key, value in ethnicities_stemms.items():
    # key is 'religion' and value is ['cathol', 'protest']
    writer = pd.ExcelWriter(key +'.xlsx', engine='xlsxwriter')
    for stemm in value:
        ethnicity_pattern = re.compile(r'.*'+stemm+'.*')
        ethnicity = expansions.loc[lambda x: x['queryterm'].str.contains(ethnicity_pattern, regex = True)]
        ethnicity.to_excel(writer, sheet_name=stemm, index=False)
        print(ethnicity)
    writer.save()

然后可以将其他键列表对添加到字典中。