简体   繁体   中英

Separate numbers from text in python

I have text data something like this:

'ABC\n',
'12.00-14.00 gm%\n',
'10.00 gm%\n',
'DEF\n',
'4000-11000 cells/cu mm.\n',
'5000 cells/cu mm\n',
'GHI\n',
'AAAA',
'40 70\n',
'83\n',
'BBB\n',
'20 40\n',
'20\n',

I want output like this:

Name  Value            Range
ABC   10.00            12.00-14.00 gm%
DEF   5000             4000-11000 cells/cu mm.
.....Same for all other values

I want output in table form. How do I do it in python ?

You can group per three items in the list and proceed as follows. Hope it helps. for example, 'ABC\\n','12.00-14.00 gm%\\n','10.00 gm%\\n' will be the first group in your iteration.

data = ['ABC\n',
'12.00-14.00 gm%\n',
'10.00 gm%\n',
'DEF\n',
'4000-11000 cells/cu mm.\n',
'5000 cells/cu mm\n',
'GHI\n',
'AAAA',
'40 70\n',
'83\n',
'BBB\n',
'20 40\n',
'20\n']

print "Name  Value            Range"
outp = [data[i].strip() + "\t" + data[i+2].strip() + "\t" +    data[i+1].strip()+'\n' for i in range(0, len(data)-3, 3)] 
for m in outp:print m 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM