環境
Windows10 Pro
Pyhoon 3.6
openpyxl 3.0.5
2つのエクセルファイルがあります。
一つは数学と英語の成績のデータ
2つ目のファイルも同じです。
この2つのエクセルファイルを一つのエクセルファイルにまとめます。
項目は同じ英語と数学です。
ヘッダーは1回目は英語と数学の項目を読み込ませて2回目以降は各セルのデータだけを処理します。
以下のコードを書きました。
import openpyxl print(openpyxl.__version__) from glob import glob wb = openpyxl.Workbook() wb.active ws = wb['Sheet'] # 変数wsの中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i+1, format_l) header= None #ヘッダーには何も入っていないと宣言しておく for file_name in glob('01_raw/*'): print(file_name) read_wb = openpyxl.load_workbook(file_name) read_ws = read_wb.worksheets[0] if not header: header = read_ws[1] header = [s.value for s in header] ws.append(header) for row in read_ws.iter_rows(min_row=2): format_l = [] for cell in row: format_l.append(cell.value) ws.append(format_l) # 中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i+1, format_l)
エラーもなく動作するのですが、
2回目、3回目のループでヘッダーを読み込んでしまいます。
ifですでのヘッダーにはデータがあるので、飛ばしてデータを処理してほしいのですが・・。
どこにミスがあるのか分かりません。
# 中身を確認する for i, row in enumerate(ws.iter_rows()): format_l = [] for cell in row: format_l.append(cell.value) print(i, format_l)
を実行すると
0 [None, 'math', 'english'] 1 ['Student1', 79, 42] 2 ['Student2', 83, 39] 3 ['Student3', 35, 72] 4 ['Student4', 63, 90] 5 ['Student5', 95, 42] 6 ['Student6', 92, 75] 7 ['Student7', 81, 85] 8 ['Student8', 68, 70] 9 ['Student9', 91, 56] 10 ['Student10', 75, 100] 11 ['Student11', 57, 91] 12 ['Student12', 94, 86] 13 ['Student13', 47, 96] 14 ['Student14', 66, 63] 15 ['Student15', 47, 37] 16 ['Student16', 42, 100] 17 ['Student17', 62, 31] 18 ['Student18', 98, 41] 19 ['Student19', 48, 81] 20 ['Student20', 69, 30] 21 ['Student1', 47, 33] 22 ['Student2', 38, 32] 23 ['Student3', 62, 33] 24 ['Student4', 45, 99] 25 ['Student5', 93, 31] 26 ['Student6', 87, 78] 27 ['Student7', 90, 57] 28 ['Student8', 78, 84] 29 ['Student9', 56, 33] 30 ['Student10', 42, 97] 31 ['Student11', 92, 58] 32 ['Student12', 33, 86] 33 ['Student13', 79, 93] 34 ['Student14', 85, 100] 35 ['Student15', 30, 59] 36 ['Student16', 87, 74] 37 ['Student17', 64, 59] 38 ['Student18', 59, 58] 39 ['Student19', 43, 88] 40 ['Student20', 70, 67] 41 [None, 'math', 'english'] 42 ['Student1', 79, 42] 43 ['Student2', 83, 39] 44 ['Student3', 35, 72] 45 ['Student4', 63, 90] 46 ['Student5', 95, 42] 47 ['Student6', 92, 75] 48 ['Student7', 81, 85] 49 ['Student8', 68, 70] 50 ['Student9', 91, 56] 51 ['Student10', 75, 100] 52 ['Student11', 57, 91] 53 ['Student12', 94, 86] 54 ['Student13', 47, 96] 55 ['Student14', 66, 63] 56 ['Student15', 47, 37] 57 ['Student16', 42, 100] 58 ['Student17', 62, 31] 59 ['Student18', 98, 41] 60 ['Student19', 48, 81] 61 ['Student20', 69, 30] 62 ['Student1', 47, 33] 63 ['Student2', 38, 32] 64 ['Student3', 62, 33] 65 ['Student4', 45, 99] 66 ['Student5', 93, 31] 67 ['Student6', 87, 78] 68 ['Student7', 90, 57] 69 ['Student8', 78, 84] 70 ['Student9', 56, 33] 71 ['Student10', 42, 97] 72 ['Student11', 92, 58] 73 ['Student12', 33, 86] 74 ['Student13', 79, 93] 75 ['Student14', 85, 100] 76 ['Student15', 30, 59] 77 ['Student16', 87, 74] 78 ['Student17', 64, 59] 79 ['Student18', 59, 58] 80 ['Student19', 43, 88] 81 ['Student20', 70, 67] 82 [None, 'math', 'english'] 83 ['Student1', 79, 42] 84 ['Student2', 83, 39] 85 ['Student3', 35, 72] 86 ['Student4', 63, 90] 87 ['Student5', 95, 42] 88 ['Student6', 92, 75] 89 ['Student7', 81, 85] 90 ['Student8', 68, 70] 91 ['Student9', 91, 56] 92 ['Student10', 75, 100] 93 ['Student11', 57, 91]
41と82でヘッダーを読んで書き込んでしまいます。if文を無視します。
回答1件
あなたの回答
tips
プレビュー