Search code examples
pythonfilecoordinatestext-manipulation

Splitting a line up into 2 different strings


So I have a file where a bunch of coordinates are in. But one x has its y value in the same line just seperated by a space. How do I split each line into two seperated pieces up so that I get the y and the x coord seperate(eg in a strig array)? Coordinate example:

934 100

Solution

  • Simply use line.split() for each string line.

    It also works on lines with more than two coordinates.

    Examples:

    • line = "934 100", x, y = line.split(), print(x,y) = 934 100

    • line = "1 61 298 3333 ", a, b, c, d = line.split(), print(a,b,c,d) = 1 61 298 3333