質問編集履歴

2

ソースコードの誤りを是正

2018/11/30 02:31

投稿

gran-1123
gran-1123

スコア18

test CHANGED
File without changes
test CHANGED
@@ -106,21 +106,21 @@
106
106
 
107
107
  results_t.append(re.findall(absobe_pattern_title, text))#results_tに格納
108
108
 
109
- results_t2.append(re.findall(absobe_pattern_title2, text))
109
+ results_t.append(re.findall(absobe_pattern_title2, text))
110
-
110
+
111
- results_t3.append(re.findall(absobe_pattern_title3, text))
111
+ results_t.append(re.findall(absobe_pattern_title3, text))
112
-
112
+
113
- results_t4.append(re.findall(absobe_pattern_title4, text))
113
+ results_t.append(re.findall(absobe_pattern_title4, text))
114
+
115
+
114
116
 
115
117
  results_ha.append(re.findall(absobe_pattern_happen, text))
116
118
 
117
-
118
-
119
- results_ha2.append(re.findall(absobe_pattern_happen2, text))
119
+ results_ha.append(re.findall(absobe_pattern_happen2, text))
120
-
120
+
121
- results_ha3.append(re.findall(absobe_pattern_happen3, text))
121
+ results_ha.append(re.findall(absobe_pattern_happen3, text))
122
-
122
+
123
- results_ha4.append(re.findall(absobe_pattern_happen4, text))
123
+ results_ha.append(re.findall(absobe_pattern_happen4, text))
124
124
 
125
125
  f.close()
126
126
 

1

回答者への更なる質問へ対応するためにソースコード変更、画像追加

2018/11/30 02:31

投稿

gran-1123
gran-1123

スコア18

test CHANGED
File without changes
test CHANGED
@@ -44,6 +44,8 @@
44
44
 
45
45
  from bs4 import BeautifulSoup
46
46
 
47
+ import pandas as pd
48
+
47
49
 
48
50
 
49
51
  results_hi = []#年号を格納しておく関数
@@ -52,24 +54,10 @@
52
54
 
53
55
  results_t = []#作品タイトルを格納しておく関数パターン1
54
56
 
55
- results_t2 = []#作品タイトルを格納しておく関数パターン2
56
-
57
- results_t3 = []#作品タイトルを格納しておく関数パターン3
58
-
59
- results_t4 = []#作品タイトルを格納しておく関数パターン4
60
-
61
57
 
62
58
 
63
59
  results_ha = []#出来事を格納しておく関数パターン1
64
60
 
65
- results_ha2 = []#出来事を格納しておく関数パターン2
66
-
67
- results_ha3 = []#出来事を格納しておく関数パターン3
68
-
69
- results_ha4 = []#出来事を格納しておく関数パターン4
70
-
71
-
72
-
73
61
 
74
62
 
75
63
  absobe_pattern_history = '^U.C.[0-9]{4}'#宇宙世紀の年号
@@ -114,21 +102,25 @@
114
102
 
115
103
  results_hi.append(re.findall(absobe_pattern_history, text))#条件に適合した文を抽出、results_hiに格納
116
104
 
105
+
106
+
117
107
  results_t.append(re.findall(absobe_pattern_title, text))#results_tに格納
118
108
 
119
- results_t2.append(re.findall(absobe_pattern_title2, text))#results_t2に格納
109
+ results_t2.append(re.findall(absobe_pattern_title2, text))
120
-
110
+
121
- results_t3.append(re.findall(absobe_pattern_title3, text))#results_t3に格納
111
+ results_t3.append(re.findall(absobe_pattern_title3, text))
122
-
112
+
123
- results_t4.append(re.findall(absobe_pattern_title4, text))#results_t4に格納
113
+ results_t4.append(re.findall(absobe_pattern_title4, text))
124
-
114
+
125
- results_ha.append(re.findall(absobe_pattern_happen, text))#results_haに格納
115
+ results_ha.append(re.findall(absobe_pattern_happen, text))
126
-
116
+
117
+
118
+
127
- results_ha2.append(re.findall(absobe_pattern_happen2, text))#results_ha2に格納
119
+ results_ha2.append(re.findall(absobe_pattern_happen2, text))
128
-
120
+
129
- results_ha3.append(re.findall(absobe_pattern_happen3, text))#results_ha3に格納
121
+ results_ha3.append(re.findall(absobe_pattern_happen3, text))
130
-
122
+
131
- results_ha4.append(re.findall(absobe_pattern_happen4, text))#results_ha4に格納
123
+ results_ha4.append(re.findall(absobe_pattern_happen4, text))
132
124
 
133
125
  f.close()
134
126
 
@@ -140,45 +132,17 @@
140
132
 
141
133
  UC_title = [t[0] for t in results_t if t != []]#抽出データから空っぽのデータを取り除いた上で、入れ子構造を解消
142
134
 
143
- UC_title2 = [t[0] for t in results_t2 if t != []]#上に同じ
144
-
145
- UC_title3 = [t[0] for t in results_t3 if t != []]#上に同じ
146
-
147
- UC_title4 = [t[0] for t in results_t4 if t != []]#上に同じ
148
-
149
135
 
150
136
 
151
137
  UC_happen = [ha[0] for ha in results_ha if ha != []]#抽出データから空っぽのデータを取り除いた上で、入れ子構造を解消
152
138
 
153
- UC_happen2 = [ha[0] for ha in results_ha2 if ha != []]#上に同じ
154
-
155
- UC_happen3 = [ha[0] for ha in results_ha3 if ha != []]#上に同じ
156
-
157
- UC_happen4 = [ha[0] for ha in results_ha4 if ha != []]#上に同じ
158
-
159
-
160
-
161
- tit = UC_title + UC_title2#作品名パターンを一つのリストに統合
162
-
163
- tit2 = tit + UC_title3
164
-
165
- tit3 = tit2 + UC_title4
166
-
167
-
168
-
169
- hap = UC_happen + UC_happen2
170
-
171
- hap2 = hap + UC_happen3
172
-
173
- hap3 = hap2 + UC_happen4
174
-
175
139
 
176
140
 
177
141
  his_list = sorted(set(UC_history), key=UC_history.index)#重複を排除
178
142
 
179
- tit_list = sorted(set(tit3), key=tit3.index)#上に同じ
143
+ tit_list = sorted(set(UC_title), key=UC_title.index)#上に同じ
180
-
144
+
181
- hap_list = sorted(set(hap3), key=hap3.index)#上に同じ
145
+ hap_list = sorted(set(UC_happen), key=UC_happen.index)#上に同じ
182
146
 
183
147
 
184
148
 
@@ -194,6 +158,28 @@
194
158
 
195
159
 
196
160
 
161
+ # 最長のリストにあわせNone要素を詰め込む
162
+
163
+ his_l,tit_l,hap_l = [],[],[]
164
+
165
+ from itertools import zip_longest
166
+
167
+ for his,tit,hap in zip_longest(his_list,tit_list,hap_list):
168
+
169
+ his_l.append(his)
170
+
171
+ tit_l.append(tit)
172
+
173
+ hap_l.append(hap)
174
+
175
+
176
+
177
+ df = pd.DataFrame({'年代':his_l,'出来事':hap_l,'作品名':tit_l},columns=['年代','出来事','作品名'])
178
+
179
+ df.to_html('ret.html',index=False)
180
+
181
+
182
+
197
183
  ```
198
184
 
199
185
  ###出力結果
@@ -221,3 +207,9 @@
221
207
  /home/urai/crawling/gandamu/gandamu uchuuseiki/1.htmlの内容は
222
208
 
223
209
  Wikipedia記事「宇宙世紀」のHTMLデータとなっております。
210
+
211
+
212
+
213
+ 解答によって年表形式で出力できるようになると思ったのですが、回答者のように出力できません。
214
+
215
+ 何が問題でしょうか?![イメージ説明](d6c898bfaf60d62dca812368cf149489.png)