读取文本文件并在Python中获取区域名称

时间:2014-12-03 23:58:54

标签: python

有一个要阅读的文本文件然后我需要在" Region"下面取名字。并将它们放在一个我遇到麻烦的列表中,就像我的代码一样,它只需要来自" Region"由于我的分裂,而不是该地区的全名,但我无法找到解决问题的方法。

文本文件前两个条目的示例。我想添加" GULF OF CALIFORNIA"和#34;离开本州的东海岸"作为列表中的一个项目,但我的代码只添加" CALIFORNIA"或" JAPAN"列表。

         MAG        UTC DATE-TIME           LAT       LON       DEPTH    Region

    MAP  4.3    2014/03/12 20:16:59       25.423     -109.730   10.0     GULF OF CALIFORNIA
    MAP  5.2    2014/03/12 20:09:55       36.747      144.050   24.2     OFF THE EAST COAST OF HONSHU, JAPAN
def main(): #defining main function
    magList = [] #magnitude list of all together
    regionList = [] #creating list to hold region names
    newRegionList = []
    with open("earthquakes.txt", "r") as eqList: #opens earthquake text file and gets the magnitudes
        eqList.readline()
        for line in eqList:
            line = line.split()
            magList.append(float(line[1])) #appends magnitude as float values in list

    with open("earthquakes.txt", "r") as eqList2:
            eqList2.readline()
            for line in eqList2:
                line = line.split()
                regionList.append(line[-1])
    newRegionList = list(set(regionList))

    greatMag = [] #creating lists for different category magnitudes
    majorMag = []
    strongMag = []
    moderateMag = []

    for x in magList: #conditions for seperating magnitude
        if x >= 8:
            greatMag.append(x)
        elif  7 <= x <= 7.9:
            majorMag.append(x)
        elif 6 <= x <= 6.9:
            strongMag.append(x)
        elif 5 <= x <= 5.9:
            moderateMag.append(x)

    print(greatMag)
    print()
    print(majorMag)
    print()    
    print(strongMag)
    print()
    print(moderateMag)
    print()
    print(regionList)


if __name__ == "__main__": #runs main function
    main()

运行我的代码后,它会添加&#34; CALIFORNIA&#34;和&#34; JAPAN&#34;到列表,但我需要整个地区名称,而不仅仅是该地区的最后一个字。

1 个答案:

答案 0 :(得分:0)

line = line.split()
line = line[0:7] + [" ".join(line[7:])]

一种简单的方法是将列表尾端的任何内容合并在一起。

您也可以通过限制返回的拆分数量来执行此操作。由于您要拆分多个空格,因此需要使用正则表达式拆分器而不是字符串拆分器:

import re
re.split('\s+', line, 8)