属性错误:“列表"对象没有属性“分割" [英] Attribute Error: 'list' object has no attribute 'split'

查看:127
本文介绍了属性错误:“列表"对象没有属性“分割"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying read a file and split a cell in each line by a comma and then display only the first and the second cells which contain information regarding the latitude and the longitude. This is the file:

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

time,latitude,longitude,type2015-03-20T10:20:35.890Z,38.8221664,-122.7649994,earthquake2015-03-20T10:18:13.070Z,33.2073333,-116.6891667,earthquake2015-03-20T10:15:09.000Z,62.242,-150.8769,earthquake

我的程序:

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()

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

When I try to execute this program, it gives me an error

"AttributeError:列表"对象没有属性拆分"

"AttributeError: 'list' object has no attribute 'split'

请帮助我!

推荐答案

我认为您实际上在这里存在更广泛的困惑.

I think you've actually got a wider confusion here.

最初的错误是您试图在整个行列表中调用split,并且您不能split一个字符串列表,只能是一个字符串.因此,您需要split 每行,而不是整个内容.

The initial error is that you're trying to call split on the whole list of lines, and you can't split a list of strings, only a string. So, you need to split each line, not the whole thing.

然后您正在执行for points in Type,并期望每个这样的points为您提供新的xy.但这不会发生. Types只是两个值,xy,所以首先points将是x,然后点将是y,然后就可以完成.因此,同样,您需要循环遍历每行并从每行获取xy值,而不是从单行遍历单个Types.

And then you're doing for points in Type, and expecting each such points to give you a new x and y. But that isn't going to happen. Types is just two values, x and y, so first points will be x, and then points will be y, and then you'll be done. So, again, you need to loop over each line and get the x and y values from each line, not loop over a single Types from a single line.

因此,所有内容都必须循环进入文件中的每一行,并对每行一次将split转换为xy.像这样:

So, everything has to go inside a loop over every line in the file, and do the split into x and y once for each line. Like this:

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语句,但是我将在最后讨论.

As a side note, you really should close the file, ideally with a with statement, but I'll get to that at the end.

有趣的是,这里的问题不在于您是个新手,还是试图以专家会用的抽象方式来解决问题,而现在还不知道细节. .这是完全可行的.您只需要明确地映射功能,而不是隐式地进行功能映射.像这样:

Interestingly, the problem here isn't that you're being too much of a newbie, but that you're trying to solve the problem in the same abstract way an expert would, and just don't know the details yet. This is completely doable; you just have to be explicit about mapping the functionality, rather than just doing it implicitly. Something like this:

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()

或者,一种更好的书写方式可能是:

Or, a better way to write that might be:

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,它们是的库,它为您提供了一种隐式地将功能映射到整个数据数组或数据帧上的方法,几乎​​与您尝试的方法相同


Finally, you may want to take a look at NumPy and Pandas, libraries which do give you a way to implicitly map functionality over a whole array or frame of data almost the same way you were trying to.

这篇关于属性错误:“列表"对象没有属性“分割"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆