0
我是一名初学者编码人员,并且我拥有的项目要求我对文本文件进行分类。 我打开的txt文件是这样的: (这并不完全如何txt文件看起来像只是当我复制并经过它时,它看起来太乱了,只有另一列刚刚填满字 '地图' 出于某种原因)在从txt文件中分组数据时遇到问题
MAG UTC DATE-TIME LAT LON DEPTH Region 4.3 2014/03/12 20:16:59 25.423 -109.730 10.0 GULF OF CALIFORNIA 5.2 2014/03/12 20:09:55 36.747 144.050 24.2 JAPAN 5.0 2014/03/12 20:08:25 35.775 141.893 24.5 JAPAN 4.8 2014/03/12 19:59:01 38.101 142.840 17.6 Japan 4.6 2014/03/12 19:55:28 37.400 142.384 24.7 JAPAN 5.0 2014/03/12 19:45:19 -6.187 154.385 62.0 GUINEA
我所要的输出是这样的:
[日本, '4.3', '5.2',” 5.0','4.8','4.6'],[加利福尼亚湾,4.3],[几内亚,5.0]]
我的当前编码: (该VLIST [7:]。在第一for循环给我的区域名称和在第二个for循环第j [1]给出我magtitude数)
def myOpen(filepointer):
header = filepointer.readline()
regions = []#gathers up all the names of the regions without repeating them
maglist = []#matchs with naems and numbers
filelines = []#list of lines in txt file
for aline in filepointer:#reades each line
vlist = aline.split()#turns lines into lists
filelines.append(vlist)
if not vlist[7:] in regions:#makes list of names without repeat
regions.append(vlist[7:])
regions.sort()
for j in filelines:#gets each file line
for names in regions:#each name
if names == j[7:]:
num = j[1]
names.append(float(num))
mags.append(names)
return maglist
def main():
myFile = open('earthquakes.txt','r')
quakes = myOpen(myFile)
myFile.close()
print(quakes)
main()
给出了这样的输出:
[日本, '4.3'],[加利福尼亚湾,4.3],[几内亚,5.0]
我不知道为什么只有它得到第一个数量级出现在其他地区,而不是其他地区。
你检查了我的代码吗? – Hackaholic 2014-12-03 10:29:41