如何读取Excel筛选CMS相关URL并按technologies分组展示?
实现Excel中CMS相关URL的分组提取
步骤1:安装依赖
首先确保安装了处理Excel的pandas库:
pip install pandas
步骤2:完整实现代码
下面是满足需求的Python代码,会自动读取Excel、筛选CMS相关条目并按技术分组:
import pandas as pd # 读取Excel文件,替换成你的文件路径 df = pd.read_excel("你的Excel文件路径.xlsx") # 定义需要匹配的CMS技术(统一小写用于判断) cms_techs = {'wordpress', 'drupal', 'joomla'} # 筛选出技术列包含指定CMS的行(不区分大小写) # 假设你的技术列名为'technologies',URL列名为'url',如果列名不同请自行修改 filtered_df = df[df['technologies'].str.lower().isin(cms_techs)] # 按统一格式的技术名称分组,收集对应的URL列表 # 把技术名称转为首字母大写的格式,避免wordpress和WordPress分成两组 grouped = filtered_df.groupby(filtered_df['technologies'].str.capitalize())['url'].apply(list).to_dict() # 输出结果 for tech, urls in grouped.items(): print(f"* {tech}:") for url in urls: print(f" - {url}") # 如果需要输出JSON格式,可使用下面的代码 # import json # print(json.dumps(grouped, indent=2))
关键细节说明
- 大小写兼容:通过
str.lower()和str.capitalize()处理技术名称的大小写问题,确保wordpress和WordPress被归为同一组 - 列名适配:代码中假设技术列名为
technologies、URL列名为url,如果你的Excel列名不同,直接替换成对应名称即可 - 结果输出:默认按列表格式打印,也可以通过JSON模块输出结构化的JSON数据,方便后续使用
示例输出效果
* WordPress: - https://example1.com - https://example2.com * Drupal: - https://example3.com * Joomla: - https://example4.com - https://example5.com
内容的提问来源于stack exchange,提问作者timlaw71
相关产品推荐
相关产品推荐

