pythonでcsvファイルで保存している表のデータを読み込み、
表側ごとに値を出力したいと考えているのですが、表頭が複数ある場合だと
うまく出力出来ずに困っています。
エラーとしては、
semi = table_side_list[hyousoku_number] + table_head_list[hyoutou_number + j]
IndexError: list index out of range
と言われていてこのバグを解消しようとしているのですがうまくいきません。
python
1from pyknp import Juman,KNP 2from joblib import Memory 3from collections import OrderedDict 4import sys 5import pandas as pd 6import tabulate 7import configparser 8from splosat.parser import semiparser 9import semiformalizer 10 11# 表側の処理をするクラス 12 13 14class TableSideProcess: 15 def __init__(self, relation, other_text): 16 self.relation = relation 17 self.other_text = other_text 18 19 def tableside(self): 20 semi_list = [] 21 semi = semiformalizer.SemiFormal(self.relation).semi().replace(').', ')->') 22 semi_split = semi.split(',') 23 semi_limit = semi_split[len(semi_split) - 1].split(')') 24 for content in self.other_text: 25 semi_list.append(semi_split[0] + ',' + semi_split[1] + ',' + semi_limit[0] + ',' + str(content) + ')' + semi_limit[1]) 26 return semi_list 27 28 29# 表頭の処理をするクラス 30class TableHeadProcess: 31 def __init__(self, dict_key): 32 self.dict_key = dict_key 33 34 def condition_text(self): 35 # 表側もしくは表頭の題名が「~中」という条件を表すものである場合 36 tableside_condition = semiformalizer.SemiFormal(self.dict_key).semi().replace('.','->') 37 return tableside_condition 38 39 40if __name__ == '__main__': 41 42 juman = Juman() 43 knp = KNP() 44 45# どういう表形式なのかを指定 46# 表題:「あり」or「なし」 47# 表側:「条件(主)」or「条件(目的)」 48# 表頭:「条件」or「結果」 49 50 # コマンドライン引数を格納したリストの取得 51 argvs = sys.argv 52 table_type = configparser.ConfigParser() 53 table_type.read(argvs[1],'utf8') 54 55 # 表のデータをcsvファイルから読み込む 56 csv_input = pd.read_csv(argvs[2], index_col = 0, sep = None, engine = 'python',encoding = 'utf8') 57 58 59 # 表頭のカテゴリ名を入れるリスト 60 hyoutou_dict_list = [] 61 62 # 表頭 63 table_head_list = [] 64 65 # 表頭のカテゴリ名をリストに入れる 66 for header in csv_input.columns: 67 hyoutou_dict_list.append(header) 68 69 # 表題をセミ形式記述に変換(表題が「条件」の場合) 70 if table_type.get('tabletype','表題') != 'なし': 71 title = semiformalizer.SemiFormal(table_type.get('tabletype','表題')).semi().replace('.', '&') 72 73 table_side_list = TableSideProcess(csv_input.index.name, csv_input.index).tableside() 74 75 # 期待値の処理 76 if table_type.get('tabletype','表頭') == '動作': 77 # if not len(hyoutou_dict_list) == 1: 78 for value_list in csv_input.values: 79 for hyoutou in value_list: 80 if hyoutou == 'なし': 81 table_head_list.append('なし') 82 else: 83 table_head_list.append(semiformalizer.SemiFormal(hyoutou).semi()) 84 85 elif table_type.get('tabletype','表頭') == '条件': 86 for value_list in csv_input.values.T: 87 for hyoutou in value_list: 88 if hyoutou == 'なし': 89 table_head_list.append('なし') 90 else: 91 table_head_list.append(semiformalizer.SemiFormal(hyoutou).semi()) 92 93 # 条件と動作のセミ形式記述を合わせる 94 i = 0 95 j = 0 96 semi_list = [] 97 for hyoutou in hyoutou_dict_list: 98 hyoutou = TableHeadProcess(hyoutou).condition_text() 99 # 表頭が1つの場合 100 if len(hyoutou_dict_list) == 1: 101 if table_head_list[hyousoku_number + i] == 'なし': 102 continue 103 elif not table_type.get('tabletype','表題') == 'なし': 104 if table_type.get('tabletype','表頭') == '条件': 105 semi = title + table_side_list[hyousoku_number].replace('.', '&') + hyoutou + table_head_list[hyousoku_number] 106 elif table_type.get('tabletype','表頭') == '動作': 107 semi = title + table_side_list[hyousoku_number] + table_head_list[hyousoku_number] 108 elif table_type.get('tabletype','表題') == 'なし': 109 if table_type.get('tabletype','表頭') == '条件': 110 semi = table_side_list[hyousoku_number].replace('.', '&') + hyoutou + table_head_list[hyousoku_number] 111 elif table_type.get('tabletype','表頭') == '動作': 112 semi = table_side_list[hyousoku_number] + table_head_list[hyousoku_number] 113 semi_list.append(semi) 114 115 # 表頭が複数ある場合 116 else: 117 for hyousoku_number in range(len(table_side_list)): 118 # 表頭のカテゴリが条件を示す場合 119 if table_type.get('tabletype','表頭') == '条件': 120 if not table_type.get('tabletype','表題') == 'なし': 121 semi = title + table_side_list[hyousoku_number].replace('->', '&') + hyoutou + table_head_list[hyousoku_number + i] 122 else: 123 semi = table_side_list[hyousoku_number].replace('->', '&') + hyoutou + table_head_list[hyousoku_number + i] 124 # 表頭のカテゴリが動作を示す場合 125 else: 126 if not table_type.get('tabletype','表題') == 'なし': 127 semi = title + table_side_list[hyousoku_number] + table_head_list[hyousoku_number + i] + table_head_list[hyousoku_number + i + len(table_side_list)] 128 else: 129 for hyoutou_number in range(len(table_head_list)): 130 semi = table_side_list[hyousoku_number] + table_head_list[hyoutou_number + j] 131 i += len(table_side_list) 132 j += len(table_head_list) 133 134 for semi in set(semi_list): 135 print(semi + '\n')
上記のような表を入力した際、それぞれの言葉を以下のように定義している
表側:「No.」の列で記載されている部分
表頭:「項目」「説明」と記載されている部分
※No.と項目・説明が交わっている部分を期待値
理想の出力としては、No.1-> 操作-> 操作方法を指示 のように行ごとに出力したい。
第三者が実行できるよう、現象が再現する最小限のコードを提示されると回答得られやすいと思います。
あなたの回答
tips
プレビュー