【问题标题】:how to use readlines()如何使用 readlines()
【发布时间】:2020-05-02 18:36:01
【问题描述】:

在运行以下代码时,出现如下错误。谁能帮我解决这个问题。

import urllib
import requests
url = "https://www.sec.gov/Archives/edgar/data/1800/000104746918000856/0001047469-18-000856.txt"
page = urllib.request.urlopen(url).read()
page.decode('utf-8')
text_list = page.decode('utf-8').readlines()

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

【问题讨论】:

  • 为什么你认为你应该在这里使用readlines?

标签: python python-3.x readlines edgar


【解决方案1】:

看起来你想要一个包含每行返回内容的列表。这段代码可以解决问题:

import urllib
import requests
url = "https://www.sec.gov/Archives/edgar/data/1800/000104746918000856/0001047469-18-000856.txt"
page = urllib.request.urlopen(url).read()
text_list = page.decode('utf-8').split('\n')

【讨论】:

    【解决方案2】:
    page.decode()
    

    返回一个字符串。 readlines() 适用于文件对象。所以如果你有一个文件对象,你会:

    linesList = fileObject.readlines()
    

    更多详情请见Python readlines description 上面的答案显示了如何使用字符串上的分隔符来获取解码页面对象的行。文件对象不是字符串,但其作用类似于文件中可能包含字符串的句柄。

    【讨论】:

      猜你喜欢
      • 2011-11-02
      • 2019-05-23
      • 1970-01-01
      • 1970-01-01
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      相关资源
      最近更新 更多