Python 属性错误:“列表”对象没有“拆分”属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30042334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 07:53:09  来源:igfitidea点击:

Attribute Error: 'list' object has no attribute 'split'

pythonlistsplitturtle-graphics

提问by loveTrumpsHate

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:

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

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

时间,纬度,经度,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,地震

My program:

我的程序:

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: 'list' object has no attribute 'split'

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

Please help me!

请帮我!

采纳答案by abarnert

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

我认为你实际上在这里有更广泛的困惑。

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

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

And then you're doing for points in Type, and expecting each such pointsto give you a new xand y. But that isn't going to happen. Typesis just two values, xand y, so first pointswill 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 xand yvalues from each line, not loop over a single Typesfrom a single line.

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

So, everything has to go inside a loop over every line in the file, and do the splitinto xand yonce for each line. Like this:

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

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

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

附带说明一下,您确实应该close将文件放入,最好带有with声明,但我会在最后进行说明。



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


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

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

回答by ODiogoSilva

The problem is that readlinesis a list of strings, each of which is a line of filename. Perhaps you meant:

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

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