回答編集履歴

1

Update

2022/03/03 04:46

投稿

melian
melian

スコア19865

test CHANGED
@@ -1,30 +1,24 @@
1
- > スライスした時と同じように抽出したいです。
2
1
  ```python
3
- import locale
4
2
  import re
5
3
 
6
4
  #ファイルパステキスト
7
5
  path = r"C:\Users\test\Desktop\test.txt"
8
6
 
9
- #ファイル読み込む
10
7
  with open(path, "r", encoding="utf-8") as f:
11
- mylist = f.readlines()
8
+ text = f.read()
12
9
 
13
- #新しいリスト作成して特定の文字を置換
14
- New_list = [re.sub(r'[\n ·\ufeff]', '', l) for l in mylist]
10
+ text = re.sub(r'[ ·\ufeff]', '', text)
11
+ block = re.findall(
12
+ r'^\d+年\d+月\d+日$(?:(?!^\d+年\d+月\d+日$).)+?Enterキーで投稿します',
13
+ text, re.DOTALL|re.MULTILINE)
15
14
 
16
- #スライスして投稿情報取得
17
- s = New_list[20:35]
15
+ New_list = [b.split('\n') for b in block]
16
+ search_words = ['#python', 'Python簡単', '#programin']
18
17
 
19
- #特定ワード(複数)が全て New_list[20:35] に含まれているかを調べる
18
+ for l in New_list:
20
- search_words = ['#python', 'Python簡単', '#programin']
21
- is_includes = all(w in s for w in search_words)
19
+ if all(w in l for w in search_words):
22
-
23
- if is_includes:
24
- print([i if (i in search_words) else '' for i in s])
25
- else:
26
- print('何もしない')
20
+ print(l)
27
21
 
28
22
  #
29
- ['', '', '', '', '', '#python', '', '#programin', '', '', '', 'Python簡単', '', '', '']
23
+ ['2022年3月1日', '', 'テスト投稿', '', 'TEST', '#python', '#code', '#programin', 'プログラミング', '簡単', 'コーディング', 'Python簡単', 'pythonできること', '', 'Enterキーで投稿します']
30
24
  ```