在从txt文件中分组数据时遇到问题

时间:2014-12-03 09:08:18

标签: python

我是初学者编码器,我的项目要求我对文本文件进行分类。 我正在打开的txt文件是这样的: (这不完全是txt文件看起来就像是当我复制并经过它时,它看起来太乱了。还有另一个专栏因为某些原因只填充了'map'这个词)

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

我希望输出是这样的:

  

[[japan,'4.3','5.2','5.0','4.8','4.6'],[加利福尼亚湾,4.3],[几内亚,5.0]]

我目前的编码: (第一个for循环中的vlist [7:]给出了区域名称,第二个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()

给出了这个输出:

  <[> [[japan,'4.3'],[加利福尼亚湾,4.3],[几内亚,5.0]]

我想知道为什么它只获得了其他地区出现的第一个数字,而不是其他地区。

1 个答案:

答案 0 :(得分:0)

您可以:使用itertools.groupbylambdamapstr.splitstr.lowerstr.join

如果您的文件如下所示:

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

这是工作代码:

>>> import itertools
>>> f = open('file.txt')
>>> [[" ".join(x),list(map(lambda z:z[0],list(y)))] for x,y in itertools.groupby(sorted(list(map(str.split,map(str.lower,list(f)[1:]))),key=lambda x:" ".join(x[6:])),key=lambda x:x[6:])]
[['guinea', ['5.0']], ['gulf of california', ['4.3']], ['japan', ['5.2', '5.0', '4.8', '4.6']]]

让我解释一下:

>>> f = open('file.txt')
>>> k = list(map(str.lower,list(f)[1:]))  # convert all lines to lower case and leave 1st line
>>> k
['4.3    2014/03/12 20:16:59       25.423     -109.730   10.0     gulf of california\n', '5.2    2014/03/12 20:09:55       36.747      144.050   24.2     japan\n', '5.0    2014/03/12 20:08:25       35.775      141.893   24.5     japan\n', '4.8    2014/03/12 19:59:01       38.101      142.840   17.6     japan\n', '4.6    2014/03/12 19:55:28       37.400      142.384   24.7     japan\n', '5.0    2014/03/12 19:45:19       -6.187      154.385   62.0     guinea\n']
>>> k = list(map(str.split,k))   # it will split the lines on whitespaces
>>> k
[['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']] 
>>> k = sorted(k,key = lambda x:" ".join(x[6:]))  # it will sort the k on Region
>>> k
[['5.0', '2014/03/12', '19:45:19', '-6.187', '154.385', '62.0', 'guinea'], ['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']]
>>> [[" ".join(x),list(map(lambda z:z[0],list(y)))] for x,y in itertools.groupby(k,key = lambda x:x[6:])]
[['guinea', ['5.0']], ['gulf of california', ['4.3']], ['japan', ['5.2', '5.0', '4.8', '4.6']]]