【问题标题】:AttributeError: 'str' object has no attribute 'text'AttributeError:“str”对象没有属性“text”
【发布时间】:2018-09-10 18:19:55
【问题描述】:

我遇到了这个错误,谁能告诉我哪里出错了。

import requests
import time
with open("C:\\temp\\cars.txt", 'r') as myfile:
  data1 = myfile.read()

searchKey = "ford="
searchEndKey = '"' 
auto = data1.text[data1.text.find(searchKey) + len(searchKey):
                  data1.text.find(searchEndKey, data1.text.find(searchKey) + 
                                                len(searchKey) + 1)]

with open("C:\\temp\\cars2.txt", "rt") as fin:
    with open("C:\\temp\\cars3.txt", "wt") as fout:
        for line in fin:
            fout.write(line.replace('ford=', 'ford='+auto))

AttributeError: 'str' 对象没有属性 'text'

【问题讨论】:

  • 为什么你认为str 会有text 属性?
  • 首先删除.text
  • 谢谢你,这很有效 - “从删除 .text 开始”

标签: python


【解决方案1】:

如上所述,删除了 .text 现在工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 2021-10-04
    • 2019-12-02
    • 2021-09-25
    相关资源
    最近更新 更多