How to split a string into line breaks in python?

I want to copy some tabular data from Excel to a python array. That is, the user will select the range in the Excel spreadsheet, click Copy (CTRL + C) so that the range is copied to the clipboard. Then I will get the data of this clipboard into an array (list) of python. I use win32clipboard from pywin32to get clipboard data in an array:

import win32clipboard

def getClip():
    win32clipboard.OpenClipboard()
    data = win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()
    return data

I will copy the following range A1:B5from Excel:

enter image description here

When I use the function above, I get a line like:

'365\t179\r\n96\t-90\r\n48\t-138\r\n12\t-174\r\n30\t-156\r\n'

How to split this line into a list so that the list looks like this:

[(365,179), (96, -90), (48, -138), (12, -174), (30, -156)]

I use the method split, but it does not give me what I want.

data.split("\n")

['365\t179\r', '96\t-90\r', '48\t-138\r', '12\t-174\r', '30\t-156\r', '']
+4
5
>>> s = '365\t179\r\n96\t-90\r\n48\t-138\r\n12\t-174\r\n30\t-156\r\n'
>>> [map(int, x.split('\t')) for x in s.rstrip().split('\r\n')]
[[365, 179], [96, -90], [48, -138], [12, -174], [30, -156]]

, :

from ast import literal_eval
def solve(x):
    try:
        return literal_eval(x)
    except (ValueError, SyntaxError):
        return x

s = '365\tFoo\r\nBar\t-90.01\r\n48\tspam\r\n12e10\t-174\r\n30\t-156\r\n'
print [map(solve, x.split('\t')) for x in s.rstrip().split('\r\n')]
#[[365, 'Foo'], ['Bar', -90.01], [48, 'spam'], [120000000000.0, -174], [30, -156]]
+6

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)]
+5
d = '365\t179\r\n96\t-90\r\n48\t-138\r\n12\t-174\r\n30\t-156\r\n'
print [tuple(map(int,item.split(","))) for item in d.replace("\t", ",").split()]

[(365, 179), (96, -90), (48, -138), (12, -174), (30, -156)]
+2
In [85]: zip(*[iter(map(int, data.split()))]*2)
Out[85]: [(365, 179), (96, -90), (48, -138), (12, -174), (30, -156)]

, :

:

In [86]: data.split()
Out[86]: ['365', '179', '96', '-90', '48', '-138', '12', '-174', '30', '-156']

ints:

In [87]: map(int, data.split())
Out[87]: [365, 179, 96, -90, 48, -138, 12, -174, 30, -156]

Use an organizer recipe to group every 2 elements:

In [88]: zip(*[iter(map(int, data.split()))]*2)
Out[88]: [(365, 179), (96, -90), (48, -138), (12, -174), (30, -156)]
+1
source
[line.split() for line in my_str.split("\n")]

It just breaks the data into lines, and then breaks them by space. Check it and change according to your data.

0
source

All Articles