There’s actually a str.splitlines method which will split the string by line breaks, regardless of which line breaks are used. So this works on Unix systems with just an \n, on Windows with \r\n and even on old Mac systems where the line break was just an \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']
Once you have this result, you can split by tabs to get the individual cells. So you essentially have to call cell.split('\t') on each cell. This is best done with a list comprehension:
>>> [row.split('\t') for row in s.splitlines()] [['365', '179'], ['96', '-90'], ['48', '-138'], ['12', '-174'], ['30', '-156']]
As an alternative, you could also use map to apply the splitting operation on each cell:
>>> list(map(lambda cell: cell.split('\t'), s.splitlines())) [['365', '179'], ['96', '-90'], ['48', '-138'], ['12', '-174'], ['30', '-156']]
As the copied data in the clipboard will always have the rows separated by newlines, and the columns separated by tabs, this solution is also safe to use for any range of cells you copied.
If you further want to convert integers or float to its correct datatypes in Python, I guess you could add some more conversion logic by calling int() on all cells that only have digits in them, float() on all cells that have digits and the dot in them ., leaving the rest as strings:
>>> 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)]
For a different string:
>>> 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)]