python - 使用特定列从 excel 创建字典

python - creating dictionary from excel using specific columns

我对 python(在 GIS 世界之外)比较陌生,并且正在努力获取代码,而我不会为我正在使用 USGS 地震数据进行的副项目获取代码。数据位于包含 7 列的 excel 文件中,如下所示:

USGS earthquake table

我有这些任务是我自己设定的:

  1. 地震总次数
  2. 用地区创建地震时间字典
  3. 地震频率最高的前 3 个位置
  4. 地震震级最高的前 3 个位置
  5. 将以上内容打印到文本文件

我被困在第二个任务上,一旦完成,我应该可以完成剩下的工作。

此外,我知道还没有任何内容打印到文本文件!

import xlrd
from xlrd import open_workbook
from collections import Counter

def get_sheet(xl_file):
    wb = open_workbook(xl_file,'r')

    # get first sheet in the workbook
    return wb.sheets()[0]

def number_eq(sheet):
    row_count = len(range(sheet.nrows))
    print ("Total number of earthqaukes = %s") % row_count

def no_earthquake_region():

    #???

#def top_freq_eq(sheet):

    #print (Counter(words).most_common(5))

#def top_mag_eq(sheet):

    #print (Counter(words).most_common(5))

def main(xl_file, out_folder):

    sheet = get_sheet(xl_file)
    row = number_eq(sheet)


if __name__ == "__main__":
    xl_file = r'D:\Projects\Other\data\EarthquakeUSGS.xlsx'
    out_folder = r"D:\Projects\Other\data\output\output.txt"
    main(xl_file, out_folder)
    print("Done!")

以下是我将采用的方法:

    import xlrd
    from xlrd import open_workbook
    from collections import Counter

    def get_sheet(xl_file):
        wb = open_workbook(xl_file,'r')

        # get first sheet in the workbook
        return wb.sheets()[0]

    def number_eq(sheet):
        row_count = len(range(sheet.nrows))
        print ("Total number of earthqaukes = %s") % row_count

    def no_earthquake_region(sheet):
        l = []
        num_of_rows = sheet.nrows
        num_of_cols = sheet.ncols
        if(num_of_rows > 1):
            d={}
            for i in range(2,num_of_rows):
                d = {"Time":sheet.cell(i,1).value , "Region":sheet.cell(i,6).value}
                l.append(d)
        return l

    #def top_freq_eq(sheet):

        #print (Counter(words).most_common(5))

    #def top_mag_eq(sheet):

        #print (Counter(words).most_common(5))

    def main(xl_file, out_folder):

        sheet = get_sheet(xl_file)
        row = number_eq(sheet)
        print no_earthquake_region(sheet)


    if __name__ == "__main__":
        xl_file = r'D:\Projects\Other\data\EarthquakeUSGS.xlsx'
        out_folder = r"D:\Projects\Other\data\output\output.txt"
        main(xl_file, out_folder)
        print("Done!")