2

I have a variable that holds the below value:

From: [email protected]
To: [email protected]
Date: Thu Sep 29 04:25:45 2016
Subject: IMAP Append Client FNBJL
MIME-version: 1.0
Content-type: text/plain; charset=UTF-8; format=flowed

  hocks burdock steelworks propellants resource querying sitings biscuits lectureship
  linearly crimea ghosting inelegant contingency resting fracas margate radiographic
  befoul waterline stopover two everlastingly highranking doctrine unsmilingly massproducing
 teacups litanies malachite pardon rarer glides nonbelievers humorously clonal tribunes
micrometer paralysing splenetic constitutionalists wavings thoughtfulness herbicide
rerolled ore overflows illicitly aerodynamics ably splittable ditching rouged bulldozer
replayed statistic reconfigured adventurers passionate rewarded decides oxygenated

Every line in the above string needs to be prepended with X: as shown below

X: From: [email protected]
X: To: [email protected]
X: Date: Thu Sep 29 04:25:45 2016
X: Subject: SSSSSSSSS FNBJL
X: MIME-version: 1.0
X: Content-type: text/plain
X: 
X:   hocks burdock steelworks propellants resource querying sitings biscuits lectureship
X:  linearly crimea ghosting inelegant contingency resting fracas margate radiographic
X:  befoul waterline stopover two everlastingly highranking doctrine unsmilingly massproducing
X:  teacups litanies malachite pardon rarer glides nonbelievers humorously clonal tribunes
X:  micrometer paralysing splenetic constitutionalists wavings thoughtfulness herbicide
X:  rerolled ore overflows illicitly aerodynamics ably splittable ditching rouged bulldozer
X:  replayed statistic reconfigured adventurers passionate rewarded decides oxygenated

I was thinking of splitting the above string on \n and prepending each line with X:

Is there a better approach for this?

2
  • 3
    "X: " + var.replace("\n", "\nX: ") Commented Sep 29, 2016 at 12:38
  • You can use a regex "replacing" ^ with X: Commented Sep 29, 2016 at 12:52

2 Answers 2

4

Use textwrap.indent from the standard library.

import textwrap

input = "This is line 1.\nThis is line 2.\n\nThis is another line."
output = textwrap.indent(input, '> ')
print(output)

output:

> This is line 1.
> This is line 2.

> This is another line.

By default, prefix is added to all lines that do not consist solely of whitespace (including any line endings).

Sign up to request clarification or add additional context in comments.

Comments

1

There are tons of ways to achieve what you want, here's few ones:

import re

log = """From: [email protected]
To: [email protected]
Date: Thu Sep 29 04:25:45 2016
Subject: IMAP Append Client FNBJL
MIME-version: 1.0
Content-type: text/plain; charset=UTF-8; format=flowed

  hocks burdock steelworks propellants resource querying sitings biscuits lectureship
  linearly crimea ghosting inelegant contingency resting fracas margate radiographic
  befoul waterline stopover two everlastingly highranking doctrine unsmilingly massproducing
 teacups litanies malachite pardon rarer glides nonbelievers humorously clonal tribunes
micrometer paralysing splenetic constitutionalists wavings thoughtfulness herbicide
rerolled ore overflows illicitly aerodynamics ably splittable ditching rouged bulldozer
replayed statistic reconfigured adventurers passionate rewarded decides oxygenated"""


def f1(text):
    return "X: " + text.replace("\n", "\nX: ")


def f2(text):
    return "X: " + re.sub('\n', '\nX: ', text)


def f3(text):
    return "\n".join(["X: {0}".format(l) for l in text.split("\n")])

if __name__ == "__main__":
    print(log)

    for f in [f1, f2, f3]:
        print('-' * 80)
        print(f(log))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.