我正在尝试以可呈现的方式格式化此数据文件。原始数据是:
6
我不知道如何摆脱每个字符串前面的空白,以及如何在下一行保存数据。
我试过了:
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
当然还有更好的方法来使用制表符空格和换行符来格式化文本吗?我错过了什么?
答案 0 :(得分:0)
当您致电line.strip()
时,您将从字符串的开头和结尾删除所有空格,包括换行符。因此,当您data = data + line.strip()
创建长字符串而没有您在打印时看到的换行符。如果你只是想在没有前导空格的情况下打印线条,你可以将其剥离:
for line in lines:
data = data + line.lstrip()
print data
或者你可以在剥离它们时打印掉剥离的线条(打印会将换行符重新插入):
for line in lines:
print line.strip()
P.S。不要在Python语句的末尾添加分号。