2015-12-21 57 views
0

我试图以可表现的方式格式化此数据文件。原始数据是:将字符串的文本文件格式化为可读

 Sodium 
      Specimen insufficient for test(s) 
     Potassium 
      Specimen insufficient for test(s) 
     Chloride 
      Specimen insufficient for test(s) 
     Bicarbonate 
      Specimen insufficient for test(s) 
     Urea 
      Specimen insufficient for test(s) 
      Authorised by xx on 04/07/2015 at 17:02 
     Creatinine        116 H umol/L     64 - 104 
     eGFR (MDRD formula)      >60  mL/min/1.73 m2 
      MDRD-derived estimation of GFR may significantly underestimate true GFR 
      in patients with GFR > 60 mL/min/1.73m^2. It may also be unreliable in 
      the case of: age <18 years or >70 years; pregnancy; serious co-morbid 
      conditions; acute renal failure; extremes of body habitus/unusual diet, 
      gross oedema. The MDRD-eGFR used here does not employ an ethnic factor 
      for race. 

      Authorised by xx on 04/07/2015 at 17:02 
     Calcium        2.44  mmol/L     2.15 - 2.55 
      Authorised by xx on 04/07/2015 at 17:02 
     Magnesium        0.88  mmol/L     0.63 - 1.05 
      Authorised by xx on 04/07/2015 at 17:02 
     Inorganic phosphate     1.47 H mmol/L     0.78 - 1.42 
      Authorised by xx on 04/07/2015 at 17:02 
     Total protein       77  g/L      60 - 78 
      Authorised by xx on 04/07/2015 at 17:02 
     Albumin         48  g/L      35 - 52 
      Authorised by xx on 04/07/2015 at 17:02 
     Total bilirubin       8  umol/L      5 - 21 
      Authorised by xx on 04/07/2015 at 17:02 
     Conjugated bilirubin (DBil)    1  umol/L      0 - 3 
     Alanine transaminase (ALT) 
      Specimen insufficient for test(s) 
      Authorised by xx on 04/07/2015 at 17:02 
     Aspartate transaminase (AST)    26  U/L      15 - 40 
      Authorised by xx on 04/07/2015 at 17:02 
     Alkaline phosphatase (ALP)    61  U/L      53 - 128 
      Authorised by xx on 04/07/2015 at 17:02 
     Gamma-glutamyl transferase (GGT)   18  U/L    <68 
     Thyroid stimulating hormone (TSH) 
      Specimen insufficient for test(s) 
     Thyroxine (free T4) 
      Specimen insufficient for test(s) 
      Authorised by xx on 02/07/2015 at 08:23 
     White Cell Count    9.75 x 109/L        3.92 - 10.40 
     Red Cell Count    5.29 x 1012/L        4.19 - 5.85 
     Haemoglobin     15.9 g/dL         13.4 - 17.5 
     Haematocrit     0.474 L/L         0.390 - 0.510 
     MCV       89.6 fL         83.1 - 101.6 
     MCH       30.0 pg         27.8 - 34.8 
     MCHC       33.5 g/dL         33.0 - 35.0 
     RDW       13.6 %         12.1 - 16.3 
     Platelet Count    217 x 109/L        171 - 388 
     MPV       10.0 fL         7.1 - 11.0 
     Neutrophils     65.60 %    6.40 x 109/L  1.60 - 6.98                32.00 - 76.00 
     Lymphocytes     25.90 %    2.53 x 109/L  1.40 - 4.20                 18.00 - 56.00 
     Monocytes     5.60 %    0.55 x 109/L  0.30 - 0.80                 4.00 - 12.00 
     Eosinophils     0.50 %    0.05 x 109/L  0.00 - 0.95                 0.00 - 8.00 
     Basophils     0.20 %    0.02 x 109/L  0.00 - 0.10                 0.00 - 2.00 
     "Other" Cells    2.10 %    0.20 x 109/L 
Mark all unread as read Mark all unviewed as viewed 
Print Report  

我不知道如何摆脱每个字符串前的空白,以及如何将数据保存到下一行。

我想:

fin = open('results.txt', "r") 
lines = fin.read() 
fin.close() 
# test it ... 
data = "" 
for line in lines: 
    data = data + line.strip(); 
print data 

但它给我:

SodiumSpecimeninsufficientfortest(s)PotassiumSpecimeninsufficientfortest(s)ChlorideSpecimeninsufficientfortest(s)BicarbonateSpecimeninsufficientfortest(s)UreaSpecimeninsufficientfortest(s)AuthorisedbyIMMofokengon04/07/2015at17:02Creatinine116Humol/L64-104eGFR(MDRDformula)>60mL/min/1.73m2MDRD-derivedestimationofGFRmaysignificantlyunderestimatetrueGFRinpatientswithGFR>60mL/min/1.73m^2.Itmayalsobeunreliableinthecaseof:age<18yearsor>70years;pregnancy;seriousco-morbidconditions;acuterenalfailure;extremesofbodyhabitus/unusualdiet,grossoedema.TheMDRD-eGFRusedheredoesnotemployanethnicfactorforrace.AuthorisedbyIMMofokengon04/07/2015at17:02Calcium2.44mmol/L2.15-2.55AuthorisedbyIMMofokengon04/07/2015at17:02Magnesium0.88mmol/L0.63-1.05AuthorisedbyIMMofokengon04/07/2015at17:02Inorganicphosphate1.47Hmmol/L0.78-1.42AuthorisedbyIMMofokengon04/07/2015at17:02Totalprotein77g/L60-78AuthorisedbyIMMofokengon04/07/2015at17:02Albumin48g/L35-52AuthorisedbyIMMofokengon04/07/2015at17:02Totalbilirubin8umol/L5-21AuthorisedbyIMMofokengon04/07/2015at17:02Conjugatedbilirubin(DBil)1umol/L0-3Alaninetransaminase(ALT)Specimeninsufficientfortest(s)AuthorisedbyIMMofokengon04/07/2015at17:02Aspartatetransaminase(AST)26U/L15-40AuthorisedbyIMMofokengon04/07/2015at17:02Alkalinephosphatase(ALP)61U/L53-128AuthorisedbyIMMofokengon04/07/2015at17:02Gamma-glutamyltransferase(GGT)18U/L<68Thyroidstimulatinghormone(TSH)Specimeninsufficientfortest(s)Thyroxine(freeT4)Specimeninsufficientfortest(s)AuthorisedbyMJModungoaon02/07/2015at08:23WhiteCellCount9.75x109/L3.92-10.40RedCellCount5.29x1012/L4.19-5.85Haemoglobin15.9g/dL13.4-17.5Haematocrit0.474L/L0.390-0.510MCV89.6fL83.1-101.6MCH30.0pg27.8-34.8MCHC33.5g/dL33.0-35.0RDW13.6%12.1-16.3PlateletCount217x109/L171-388MPV10.0fL7.1-11.0Neutrophils65.60%6.40x109/L1.60-6.9832.00-76.00Lymphocytes25.90%2.53x109/L1.40-4.2018.00-56.00Monocytes5.60%0.55x109/L0.30-0.804.00-12.00Eosinophils0.50%0.05x109/L0.00-0.950.00-8.00Basophils0.20%0.02x109/L0.00-0.100.00-2.00"Other"Cells2.10%0.20x109/LMarkallunreadasreadMarkallunviewedasviewedPrintReport 

,肯定那里有一个更好的方式来使用标签空格和换行来格式化文本?我错过了什么?

+1

提示:如果你想'数据'多于一行,你可能需要一个'+“\ n”'。 – Kevin

回答

0

当您拨打line.strip()时,您将从字符串的开头和结尾处删除所有空白,包括换行符。所以当你data = data + line.strip()你正在创建没有换行的长字符串时,你看到它时,你打印它。如果你只是想打印的行没有的留白,你可以,你既可以只剥除其关闭:

for line in lines: 
    data = data + line.lstrip() 
print data 

或者你可以只打印出剥离行,你带他们(印刷将提出一个新行回):

for line in lines: 
    print line.strip() 

PS不要把分号放在Python语句的末尾。

相关问题