私は次のコードを持っています:
pat = re.compile(r'^(\d+\/\d+\/\d+,\s\d+:\d+\s\w+\s-\s)', re.S | re.M)
with open(r'C:\Users\usamahaider\Downloads\mmm.txt', encoding="utf8") as f:
mylist = [m.group(1) for m in pat.finditer(f.read())]
print(mylist)
出力は次のとおりです。
['12/30/19, 8:57 AM - ', '12/3/19, 14:57 AM - ', '9/20/19, 8:52 AM - ', '12/3/19, 8:57 AM - ', '12/3/19, 9:34 PM - ', '12/3/19, 9:34 PM - ', '12/4/19, 6:45 AM - ', '12/4/19, 6:49 AM - ', '12/4/19, 7:12 AM - ', '12/4/19, 7:19 AM - ', '12/4/19, 7:20 AM - ', '12/4/19, 7:34 AM - ', '12/4/19, 8:00 AM - ', '12/4/19, 9:45 AM - ', '12/4/19, 10:15 AM - ', '12/4/19, 10:55 AM - ']
これはパターンを返すだけですが、すべてのテキストを単一のパターンに関連付けたいと思います。
このようなもの:
['12/30/19, 8:57 AM -Messages and calls are end-to-end encrypted. No one outside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more. ', '12/3/19, 14:57 AM - You joined using this group's invite link', '9/20/19, 8:52 AM - (347) 599-6911 created group "Sunnah Marriage Group 1"']
テキストファイルは次のようになります。
12/30/19, 8:57 AM - Messages and calls are end-to-end encrypted. No one
outside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more.
12/3/19, 14:57 AM - You joined using this group's invite link
9/20/19, 8:52 AM - (347) 599-6911 created group "Sunnah Marriage Group 1"
12/3/19, 8:57 AM - You joined using this group's invite link
12/3/19, 9:34 PM - +1 (516) 343-8410: Gender: Female
Height: 5’ 8”
Age: 21
Education: 1st Yr Medical School
Profession: Future Doctor
Marital status: Never married
Ethnicity: Pakistani
Religious background: Sunni
Family: Parents, Brothers, Sister
Language: English, Urdu
Hobbies: Travel, Art, Reading
LOOKING FOR:
Age : 24-29
Height: 5’ 10” or taller
Religion: Sunni Muslim
Education: MD/DO
Profession: Doctor/ Medical Residency/Medical Student
Marital Status: Never married
Contact: Mother
WhatsApp: (647) 879-1400
12/3/19, 9:34 PM - +1 (516) 343-8410: <Media omitted>
12/4/19, 6:45 AM - (347) 599-6911 changed this group's settings to allow all participants to send messages to this group
12/4/19, 6:49 AM - (347) 599-6911: As Salamualikum warahmatullah. Please Post and forward practicing muslims and your profiles in order to remain in the group. You have 1 day to post it until settings changes again. Strictly No chatting and no picture in the group. Please contact interested candidates in private. JazakAllahu Khairn. May Allah make halal easy for all the believers....Ameen
使用する
re.split(r'^(?=\d+/\d+/\d+,\s\d+:\d+\s+\w+\s+-\s)', string, flags=re.M)
証明を参照してください。
Pythonの 証明:
import re
string = """12/30/19, 8:57 AM - Messages and calls are end-to-end encrypted. No one \n\noutside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more.\n12/3/19, 14:57 AM - You joined using this group's invite link\n9/20/19, 8:52 AM - (347) 599-6911 created group "Sunnah Marriage Group 1"\n12/3/19, 8:57 AM - You joined using this group's invite link\n\n12/3/19, 9:34 PM - +1 (516) 343-8410: Gender: Female\nHeight: 5’ 8”\nAge: 21\nEducation: 1st Yr Medical School\nProfession: Future Doctor\nMarital status: Never married\nEthnicity: Pakistani\nReligious background: Sunni\nFamily: Parents, Brothers, Sister\nLanguage: English, Urdu\nHobbies: Travel, Art, Reading\n\nLOOKING FOR: \nAge : 24-29\nHeight: 5’ 10” or taller\nReligion: Sunni Muslim \nEducation: MD/DO\nProfession: Doctor/ Medical Residency/Medical Student \nMarital Status: Never married \n\nContact: Mother\nWhatsApp: (647) 879-1400\n12/3/19, 9:34 PM - +1 (516) 343-8410: <Media omitted>\n12/4/19, 6:45 AM - (347) 599-6911 changed this group's settings to allow all participants to send messages to this group\n12/4/19, 6:49 AM - (347) 599-6911: As Salamualikum warahmatullah. Please Post and forward practicing muslims and your profiles in order to remain in the group. You have 1 day to post it until settings changes again. Strictly No chatting and no picture in the group. Please contact interested candidates in private. JazakAllahu Khairn. May Allah make halal easy for all the believers....Ameen"""
results = list(filter(None, re.split(r'^(?=\d+/\d+/\d+,\s\d+:\d+\s+\w+\s+-\s)', string, flags=re.M)))
for line in results: print('====',line.strip())
結果:
==== 12/30/19, 8:57 AM - Messages and calls are end-to-end encrypted. No one
outside of this chat, not even WhatsApp, can read or listen to them. Tap to learn more.
==== 12/3/19, 14:57 AM - You joined using this group's invite link
==== 9/20/19, 8:52 AM - (347) 599-6911 created group "Sunnah Marriage Group 1"
==== 12/3/19, 8:57 AM - You joined using this group's invite link
==== 12/3/19, 9:34 PM - +1 (516) 343-8410: Gender: Female
Height: 5’ 8”
Age: 21
Education: 1st Yr Medical School
Profession: Future Doctor
Marital status: Never married
Ethnicity: Pakistani
Religious background: Sunni
Family: Parents, Brothers, Sister
Language: English, Urdu
Hobbies: Travel, Art, Reading
LOOKING FOR:
Age : 24-29
Height: 5’ 10” or taller
Religion: Sunni Muslim
Education: MD/DO
Profession: Doctor/ Medical Residency/Medical Student
Marital Status: Never married
Contact: Mother
WhatsApp: (647) 879-1400
==== 12/3/19, 9:34 PM - +1 (516) 343-8410: <Media omitted>
==== 12/4/19, 6:45 AM - (347) 599-6911 changed this group's settings to allow all participants to send messages to this group
==== 12/4/19, 6:49 AM - (347) 599-6911: As Salamualikum warahmatullah. Please Post and forward practicing muslims and your profiles in order to remain in the group. You have 1 day to post it until settings changes again. Strictly No chatting and no picture in the group. Please contact interested candidates in private. JazakAllahu Khairn. May Allah make halal easy for all the believers....Ameen
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加