【问题标题】:Attribute Error: 'list' object has no attribute 'split'属性错误:“列表”对象没有属性“拆分”
【发布时间】:2015-07-14 13:25:11
【问题描述】:

我正在尝试读取文件并用逗号分隔每行中的一个单元格,然后仅显示第一个和第二个单元格,其中包含有关纬度和经度的信息。 这是文件:

时间,纬度,经度,type2015-03-20T10:20:35.890Z,38.8221664,-122.7649994,earthquake2015-03-20T10:18:13.070Z, 33.2073333,-116.6891667,地震2015-03-20T10:15:09.000Z,62.242,-150.8769,地震

我的程序:

def getQuakeData():
    filename = input("Please enter the quake file: ")
    readfile = open(filename, "r")
    readlines = readfile.readlines()

    Type = readlines.split(",")
    x = Type[1]
    y = Type[2]
    for points in Type:
        print(x,y)
getQuakeData()

当我尝试执行这个程序时,它给了我一个错误

"AttributeError: 'list' 对象没有属性 'split'

请帮帮我!

【问题讨论】:

  • 因为list 没有split() 只有string 对象有split
  • readlines() 返回一个list。您必须在该list 中的每个字符串上使用split()
  • 仔细阅读错误。您正在尝试拆分 readlines,这是一个列表。那是做不到的。
  • 你为什么首先使用readlines?如果您只想将第一行作为一个字符串,那就是readline。如果您希望将整个文件作为一个巨大的字符串,那就是read。如果你想要一个列表或其他可迭代的行,你可以为此使用readlines,但你也可以只使用文件本身,所以它仍然没有用。
  • 附带说明,您可能需要考虑在此处使用csv 模块。特别是如果实际上有标题行;那么你可以使用for row in csv.DictReader(readfile):,然后你的值是row['latitude']row['longitude']而不是row[1]row[2],这可能更具可读性。

标签: python list split turtle-graphics


【解决方案1】:

我认为您实际上在这里遇到了更广泛的困惑。

最初的错误是你试图在整个行列表上调用split,而你不能split 一个字符串列表,只有一个字符串。所以,你需要split每一行,而不是全部。

然后你正在做for points in Type,并期望每个这样的points 给你一个新的xy。但这不会发生。 Types 只是两个值,xy,所以首先 points 将是 x,然后积分将是 y,然后你就完成了。因此,再次,您需要遍历每一行并从 每一行 中获取 xy 值,而不是从单行遍历单个 Types

因此,所有内容都必须在文件中每一行的循环中进行,并对每一行执行一次splitxy。像这样:

def getQuakeData():
    filename = input("Please enter the quake file: ")
    readfile = open(filename, "r")

    for line in readfile:
        Type = line.split(",")
        x = Type[1]
        y = Type[2]
        print(x,y)

getQuakeData()

附带说明一下,您确实应该close 文件,最好使用with 声明,但我会在最后谈到这一点。


有趣的是,这里的问题不是您过于新手,而是您试图以与专家相同的抽象方式解决问题,只是还不知道细节.这是完全可行的;您只需要明确映射功能,而不仅仅是隐式执行。像这样的:

def getQuakeData():
    filename = input("Please enter the quake file: ")
    readfile = open(filename, "r")
    readlines = readfile.readlines()
    Types = [line.split(",") for line in readlines]
    xs = [Type[1] for Type in Types]
    ys = [Type[2] for Type in Types]
    for x, y in zip(xs, ys):
        print(x,y)

getQuakeData()

或者,更好的写作方式可能是:

def getQuakeData():
    filename = input("Please enter the quake file: ")
    # Use with to make sure the file gets closed
    with open(filename, "r") as readfile:
        # no need for readlines; the file is already an iterable of lines
        # also, using generator expressions means no extra copies
        types = (line.split(",") for line in readfile)
        # iterate tuples, instead of two separate iterables, so no need for zip
        xys = ((type[1], type[2]) for type in types)
        for x, y in xys:
            print(x,y)

getQuakeData()

最后,您可能想看看 NumPy 和 Pandas,它们确实为您提供了一种在整个数组或数据帧上隐式映射功能的方法,几乎​​与您尝试的方式相同到。

【讨论】:

    【解决方案2】:

    问题是readlines是一个字符串列表,每个字符串都是filename的一行。也许你的意思是:

    for line in readlines:
        Type = line.split(",")
        x = Type[1]
        y = Type[2]
        print(x,y)
    

    【讨论】:

      【解决方案3】:

      我所做的是通过将 readlines 转换为字符串来快速修复,但我没有重新开始它,但它有效,我不知道是否有限制

      `def getQuakeData():
          filename = input("Please enter the quake file: ")
          readfile = open(filename, "r")
          readlines = str(readfile.readlines())
      
          Type = readlines.split(",")
          x = Type[1]
          y = Type[2]
          for points in Type:
              print(x,y)
      getQuakeData()`
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-14
        • 2015-01-12
        • 2021-05-25
        • 2022-01-20
        • 1970-01-01
        • 1970-01-01
        • 2020-05-07
        相关资源
        最近更新 更多