[英]Attribute Error: 'list' object has no attribute 'split'
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!请帮我!
I think you've actually got a wider confusion here.我认为你实际上在这里有更广泛的困惑。
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.最初的错误是您试图在整个行列表上调用
split
,并且您不能split
字符串列表,只能split
字符串。 So, you need to split
each line , not the whole thing.所以,你需要
split
每一行,而不是整件事。
And then you're doing for points in Type
, and expecting each such points
to give you a new x
and y
.然后你正在做
for points in Type
,并期望每个这样的points
给你一个新的x
和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. Types
只是两个值, x
和y
,所以第一个points
是x
,然后点是y
,然后你就完成了。 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.因此,同样,您需要遍历每一行并从每行获取
x
和y
值,而不是从一行中遍历单个Types
。
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.因此,所有内容都必须在文件中的每一行上进行循环,并对每一行进行一次
x
和y
split
。 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()
As a side note, you really should close
the file, ideally with a with
statement, 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 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.最后,您可能想看看 NumPy 和 Pandas,它们确实为您提供了一种隐式映射整个数组或数据帧的功能的方法,这与您尝试的方式几乎相同。
The problem is that readlines
is a list of strings, each of which is a line of filename
.问题是
readlines
是一个字符串列表,每个字符串都是一行filename
。 Perhaps you meant:也许你的意思是:
for line in readlines:
Type = line.split(",")
x = Type[1]
y = Type[2]
print(x,y)
what i did was a quick fix by converting readlines to string but i do not recommencement it but it works and i dont know if there are limitations or not我所做的是通过将 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()`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.