str.splitlines, , , . , Unix- \n, Windows \r\n Mac, \r.
>>> s = '365\t179\r\n96\t-90\r\n48\t-138\r\n12\t-174\r\n30\t-156\r\n'
>>> s.splitlines()
['365\t179', '96\t-90', '48\t-138', '12\t-174', '30\t-156']
, , . cell.split('\t') . :
>>> [row.split('\t') for row in s.splitlines()]
[['365', '179'], ['96', '-90'], ['48', '-138'], ['12', '-174'], ['30', '-156']]
map :
>>> list(map(lambda cell: cell.split('\t'), s.splitlines()))
[['365', '179'], ['96', '-90'], ['48', '-138'], ['12', '-174'], ['30', '-156']]
, , , , .
float Python, , , int() , , float() , ., :
>>> def convert (cell):
try:
return int(cell)
except ValueError:
try:
return float(cell)
except ValueError:
return cell
>>> [tuple(map(convert, row.split('\t'))) for row in s.splitlines()]
[(365, 179), (96, -90), (48, -138), (12, -174), (30, -156)]
:
>>> s = 'Foo\tbar\r\n123.45\t42\r\n-85\t3.14'
>>> [tuple(map(convert, row.split('\t'))) for row in s.splitlines()]
[('Foo', 'bar'), (123.45, 42), (-85, 3.14)]