2016-09-29 17 views
0

Iは、以下の値を保持する変数を有する:上記の文字列のすべての行が先頭に追加文字列のPythonの各行

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下に示されているようX:と付加する必要

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 

上記の文字列を \nに分割し、各行の先頭にそれぞれ X:

、 、 、

を追加することをお勧めしますか?

+3

' "X:" + var.replace( "\ nを"、 "\ nXを:")' –

+0

することができます正規表現を使って '' '' '' ''を '' X: ''に置き換えてください。 – allo

+0

@VincentSavard、Looks Ok。 – blackpen

答えて

1

あなたが望むものを達成するための方法のトンがありますが、ここではいくつかのものです:

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)) 
関連する問題