0

I have the following content in a text file.

Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.com

Subject: Finish setting up your new Google Account

From: Google Community Team <googlecommunityteam-noreply@google.com>

To: example@email.com

Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.com

I would like to store the first three lines in a tuple, and next 3 lines in an another tuple and so on, like below. [expected output]

['Subject: Security alert', 'From: Google <no-reply@accounts.google.com>', 'To: example@email.com']
['Subject: Finish setting up your new Google Account', 'From: Google Community Team <googlecommunityteam-noreply@google.com>', 'To: example@email.com']
['Subject: Security alert', 'From: Google <no-reply@accounts.google.com>', 'To: example@email.com']

I tried with the following code, however I am missing in how to take "each line" rather than "each word" as I tried below.

with open('input.txt') as f:
     result = map(str.split, f)
     t = tuple(result)
     print(t)

# Unexpected output
(['Subject:', 'Security', 'alert'], [], ['From:', 'Google', '<no-reply@accounts.google.com>'], [], ['To:', 'pavan.python1393@gmail.com'], [], ['Subject:', 'Finish', 'setting', 'up', 'your', 'new', 'Google', 'Account'], [], ['From:', 'Google', 'Community', 'Team', '<googlecommunityteam-noreply@google.com>'], [], ['To:', 'pavan.python1393@gmail.com'], [], ['Subject:', 'Security', 'alert'], [], ['From:', 'Google', '<no-reply@accounts.google.com>'], [], ['To:', 'pavan.python1393@gmail.com'], [])
martineau
  • 119,623
  • 25
  • 170
  • 301
smc
  • 2,175
  • 3
  • 17
  • 30

2 Answers2

1

This preserves the lines between. That why it grabs six lines instead of 3.

text="""Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.com

Subject: Finish setting up your new Google Account

From: Google Community Team <googlecommunityteam-noreply@google.com>

To: example@email.com

Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.com"""


lines = text.split('\n')
emails=[]

while lines:
    bunch=lines[:6]
    (esubj,efrom,eto)=bunch[0],bunch[2],bunch[4]
    e=(esubj,efrom,eto)
    print(e)
    assert "ubject" in esubj and "rom" in efrom and "To:" in eto
    emails.append((e))
    lines=lines[6:]
print(emails)
shanecandoit
  • 581
  • 1
  • 3
  • 11
  • As I see you've assumed the lines are only 6. But the number of lines may be more than 6 lines (e.g. 12 lines) – smc Jan 23 '20 at 19:10
  • No, it works through a list of any length, taking up the first six lines at a time. 100 lines or more will work fine. – shanecandoit Jan 23 '20 at 19:12
0

Here You go

text = """Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.com

Subject: Finish setting up your new Google Account

From: Google Community Team <googlecommunityteam-noreply@google.com>

To: example@email.com

Subject: Security alert

From: Google <no-reply@accounts.google.com>

To: example@email.co"""


data = list(filter(bool, text.splitlines()))

def divide_data(l, n):
    for i in range(0, len(l), n):
        yield l[i:i + n]

result = list(divide_data(data, text.count('Subject:')))

Result:

[['Subject: Security alert', 'From: Google <no-reply@accounts.google.com>', 'To: example@email.com'], ['Subject: Finish setting up your new Google Account', 'From: Google Community Team <googlecommunityteam-noreply@google.com>', 'To: example@email.com'], ['Subject: Security alert', 'From: Google <no-reply@accounts.google.com>', 'To: example@email.co']]
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38