xlrdライブラリを使用してテキスト形式オプションで市町村の数に関する公式データを読み取る

ある公共プロジェクト(集落と地域の予算の視覚化)では、市町村の階層とその数に関するデータが必要でした。

必要なデータは見つかりましたが、それらを正しく使用するためのプレゼンテーションの形式には、多くの要望がありました。

必要なデータはすべて1つのファイルに収められていましたが、そのフォーマットにより階層の抽出が困難になりました。 市町村、地区、および地域の名前はすべて1つの列にあり、セルとフォントの形式のみが異なります。 地域は太字で、地域は太字で、居住地はインデントされています。 また、ファイルにはいくつかのエラーがありました。たとえば、ある領域が領域として強調表示された(またはその逆、もう覚えていない)、フォーメーションの名前の真ん中の別の場所でラインクロッシングが発生しました(この瞬間は、dbのGoogle App Engineのインポート段階で明らかになりました) StringProperty()は複数行で宣誓しました)、これらの場所のソースファイルを修正する必要がありました。



これを行う最良の方法の解決策を探して、xlrdライブラリに出会いました。xlrdライブラリの機能はこのタスクに十分すぎるほどです。 詳細については、 こちらをご覧ください 。ただし、インポートプログラムのコードと、書式設定を行う際に使用可能なパラメーターを示します。

セルの書式設定とフォントタイプは、次の情報を提供します。

セル
アラインメント(XFAlignmentオブジェクト):

hor_align:0

indent_level:0

回転:0

shrink_to_fit:0

text_direction:0

text_wrapped:0

vert_align:2

背景(XFBackgroundオブジェクト):

background_colour_index:65

fill_pattern:0

pattern_colour_index:64

ボーダー(XFBorderオブジェクト):

bottom_colour_index:64

bottom_line_style:7

diag_colour_index:0

diag_down:0

diag_line_style:0

diag_up:0

left_colour_index:64

left_line_style:1

right_colour_index:64

right_line_style:1

top_colour_index:64

top_line_style:1

font_index:7

format_key:0

is_style:0

lotus_123_prefix:0

parent_style_index:0

保護(XFProtectionオブジェクト):

cell_locked:1

formula_hidden:0

xf_index:556


フォント
struck_out:0

underline_type:0

下線付き:0

重さ:400

太字:1

character_set:204

color_index:32767

escapement_type:0

家族:0

font_index:10

高さ:200

斜体:1

名前:u'Arial Cyr '

アウトライン:0

影:0


私はすべてのパラメーターを理解していませんでした;このタスクでは、セル内のフォントタイプとインデント(ident)で十分でした。 ただし、特定のセルの境界線や線の種類の色まで、可能性が大きいことは明らかです。



スクリプトコード
# -*- coding: utf-8 -*- from __future__ import unicode_literals import xlrd import json rb = xlrd.open_workbook('Tabl-35-12.xls', formatting_info=True) font_list = rb.font_list # list of all fonts in excel table sheet = rb.sheet_by_index(1) rows_number = sheet.nrows peoples_dict = {} # main dict for rownum in range(7, rows_number): # data starts with line 7 cell = sheet.cell(rownum, 0) value = cell.value.strip().replace('\n', ' ') # delete spaces at start and end peoples_count = sheet.cell(rownum, 1).value if peoples_count == 0 or peoples_count == '': # empty row - continue continue peoples_count = int(peoples_count) # from 12313.0 to integer cell_format = rb.xf_list[cell.xf_index] current_font = font_list[cell_format.font_index] bold = bool(current_font.bold) italic = bool(current_font.italic) indent = cell_format.alignment.indent_level is_region = bold and not italic is_raion = bold and italic is_municipal = (indent == 2) if is_region: region = value peoples_dict[region] = {'count': peoples_count} elif is_raion: raion = value peoples_dict[region][raion] = {'count': peoples_count} elif is_municipal: municipal = value peoples_dict[region][raion][municipal] = {'count': peoples_count} print peoples_dict[' ']['  ']['  ']['count'] with open('peoples.json', 'w') as outfile: json.dump(peoples_dict, outfile)
      
      









ボーナスとして-結果の出力ファイル(json形式) 。 構造-ネストされた辞書。各要素にはキー 'count'が含まれ、教育の数が記録され、その子孫のキーが含まれます。

つまり、モスクワ地域の番号は次のようにして取得できます。

 peoples_dict[' ']['count']
      
      





また、イストラ市のイストラ市の規模は次のとおりです。

 peoples_dict[' ']['  ']['  ']['count']
      
      






All Articles