Convert to polygons and points the values of a file in Python

2

I have a file on my PC, text.txt that contains these values

5 0, 6 1, 5 2, 4 1 | 5 1
1 1, 4 1, 4 4, 1 4 | 3 3
1 1, 3 3, 4 1, 3 0 | 3 4

and I need to convert to polygons all the values before the symbol |, and to points the last 2 values, correct output would be like this:

poligonos = [[(5,0), (6, 1), (5, 2), (4, 1)],
               [(1,1), (4,1), (4, 4), (1, 4)],
               [(1, 1), (3, 3), (4, 1), (3, 0)]
              ]

puntos = [[(5,1)],[(3,3)],[(3,4)]]

In my advance I try to recover polygons like this:

texto = 'texto.txt'
archivo = open(texto)
if archivo is not None:
    poligonos = []
    puntos = []
    valores = archivo.readlines()
    for x in valores:
        a1, a2 = x.split('|')
        poligonos.append(a1.strip().split(' '), a2.strip().split(' '))
    print(poligonos)

and in the poligonos.append(a1.strip().split(' '), a2.strip().split(' ')) part it throws TypeError: append() takes exactly one argument (2 given)

What would be the way to separate into polygons and points? Thanks

    
asked by Alex Ancco Cahuana 18.12.2017 в 15:15
source

1 answer

2

As the error itself tells you, you are trying to add two items to the list using list.append . This method only receives one parameter and adds this parameter as an element to the end of the list, to add more than one you must use list.extend , which receives an iterable as an argument.

Anyway you're trying to add both the point and the vertices to the polígonos list. Also, you should separate the value x e y from each coordinate and make a casting to % co_of% of them.

You can do something like:

poligonos, puntos = [], []
with open ("texto.txt") as f:
    for line in f:
        vertices, punto = line.split(" | ")
        poligonos.append([tuple(int(c) for c in  vertice.split())
                                           for vertice in vertices.split(", ")])
        puntos.append([tuple(int(c) for c in punto.split())])

For each row we therefore make the following steps:

  • We separate the part of the string that contains the vertices from the one that contains the point. For this we use "|" as a separator:

    >>> linea = "1 1, 3 3, 4 1, 3 0 | 3 4"
    >>> vertices, punto = line.split(" | ")
    >>> vertices
    "1 1, 3 3, 4 1, 3 0"
    >>> punto
    "3 4"
    
  • We obtain each pair of coordinates of each vertex, for this we separate by ",":

     >>> vertices = "1 1, 3 3, 4 1, 3 0"
     >>> vertices.split(", ")
     ['1 1', '3 3', '4 1', '3 0']
    
  • Now we have separated each vertex but we want to separate the coordinates int , e x and store them in a tuple. We must also cast each coordinate at y to then operate with them without problems:

    >>> vertices = ['1 1', '3 3', '4 1', '3 0']
    >>> [vertice.split() for vertice in vertices]
    [['1', '1'], ['3', '3'], ['4', '1'], ['3', '0']]
    
    >>> [int(coord) for coord in vertice.split() for vertice in vertices]
    [[1, 1], [3, 3], [4, 1], [3, 0]]
    
    >>> [tuple(int(coord) for coord in vertice.split()) for vertice in vertices]
    [(1, 1), (3, 3), (4, 1), (3, 0)]
    
  • Finally, the point is treated like a vertex.

Exit:

>>> poligonos
[[(5, 0), (6, 1), (5, 2), (4, 1)],
 [(1, 1), (4, 1), (4, 4), (1, 4)],
 [(1, 1), (3, 3), (4, 1), (3, 0)]]

>>> puntos
[[(5, 1)],
 [(3, 3)],
 [(3, 4)]]
    
answered by 18.12.2017 / 16:00
source