You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在TXT文本中查找含xml的文件名?解决AttributeError报错

问题解决:提取文本中含"xml"的文件名

错误原因

你代码里的var_split[i]是普通字符串对象,但调用了仅适用于pandas Series/DataFrame的.str.contains()和.iloc[]方法,因此触发AttributeError。

修正后的代码

无需引入pandas,用Python原生字符串操作即可实现需求:

local_arquivo = "你的文件路径.txt"  # 替换为实际文件路径
archive_name = ""

with open(local_arquivo, "r") as file_read:
    for line in file_read:
        var_split = line.split()
        for word in var_split:
            if "xml" in word:
                archive_name = word
                break  # 找到目标后跳出循环,提升效率
        if archive_name:
            break

print(f"archive_name = {archive_name}")

代码说明

  • 用Python原生的in操作符检查字符串是否包含"xml"
  • 遍历分割后的每个单词,匹配成功后直接赋值并终止循环
  • 不依赖第三方库,轻量高效

内容的提问来源于stack exchange,提问作者Jane Borges

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 12:45:52