Python 属性错误:';列表';对象没有属性';拆分';
我正在尝试读取一个文件并用逗号分隔每行中的一个单元格,然后仅显示第一个和第二个单元格,其中包含有关纬度和经度的信息。 这是文件: 时间,纬度,经度,类型2015-03-20T10:20:35.890Z,38.8221664,-122.7649994,地震2015-03-20T10:18:13.070Z,33.207333,-116.6891667,地震2015-03-20T10:15:09.000Z,62.242,-150.8769,地震 我的节目:Python 属性错误:';列表';对象没有属性';拆分';,python,list,split,turtle-graphics,Python,List,Split,Turtle Graphics,我正在尝试读取一个文件并用逗号分隔每行中的一个单元格,然后仅显示第一个和第二个单元格,其中包含有关纬度和经度的信息。 这是文件: 时间,纬度,经度,类型2015-03-20T10:20:35.890Z,38.8221664,-122.7649994,地震2015-03-20T10:18:13.070Z,33.207333,-116.6891667,地震2015-03-20T10:15:09.000Z,62.242,-150.8769,地震 我的节目: def getQuakeData():
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:'列表'对象没有属性'split'
请帮帮我!问题在于
readlines
是一个字符串列表,每个字符串都是文件名的一行。您的意思可能是:
for line in readlines:
Type = line.split(",")
x = Type[1]
y = Type[2]
print(x,y)
我想你实际上有更大的困惑
最初的错误是,您试图对整个行列表调用split
,而不能split
字符串列表,只能是字符串。因此,您需要split
每行,而不是整行
然后你对类型中的点做,并期望每个点给你一个新的x
和y
。但这不会发生。Type
只是两个值,x
和y
,所以首先点将是x
,然后点将是yde>,然后就可以完成了。所以,同样,您需要在每一行上循环,并从每一行中获取x
和y
值,而不是从一行中循环单个类型
因此,所有内容都必须进入文件中每一行的循环中,并对每一行执行一次拆分成x
和y
。如下所示:
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()
作为旁注,您确实应该关闭
文件,最好使用with
语句,但我将在最后讨论这个问题
有趣的是,这里的问题不是你太新手了,而是你正试图用专家会用的抽象方式解决问题,只是还不知道细节。这是完全可行的;你只需要明确地映射功能,而不是隐式地做。类似这样的事情s:
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库,它们确实为您提供了一种隐式映射整个数组或数据帧功能的方法,几乎与您尝试的方法相同。我所做的是快速修复,将读取行转换为字符串,但我没有重新启动它,但它可以工作,我不知道是否有限制不管你是不是
`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()`
因为list
没有split()
只有string
对象有split
readlines()
返回一个list
。您必须使用split()
在列表中的每个字符串上。仔细阅读错误。您正在尝试拆分读线
,这是一个列表。这是无法完成的。为什么您首先要使用读线
?如果您只想将第一行作为一个字符串,那就是读线
。如果您想将整个文件作为一个巨大的字符串,那就是<代码>读取< /代码>。如果您想要一个列表或其他可重复的行,您可以使用 RealStult<代码>,但是您也可以仅使用文件本身,因此它仍然没有用处。作为一个旁注,您可能需要考虑使用<代码> CSV 模块。特别是如果实际上有一个标题行;那么您可以在CSV.DI中使用<代码>行。cReader(readfile):
然后您的值是行['latitude']
和行['latitude']
,而不是行[1]
和行[2]
,这可能更具可读性。