how can I sort the following data using regex in python?

0

I have a file that has the following structure:

Nombre1 = Funcion(New func("")) 'comentarios
Nombre2 = Funcion(New func("")) 'comentarios
Nombre3 = Funcion(New func("")) 'comentarios

I need that where there is double quote or double quotes, put it like that \"\" To part I need to remove all the comments and that the result file is such that:

Nombre1 = Funcion(Texto Valor(\"\")):Nombre2 = Funcion(New func(\"\")):Nombre3 = Funcion(New func(\"\"))

All of my variables will be on a single line and will eliminate all comments; In addition to line breaks, the result will be as mentioned above. Is it possible to do this using regex in python so you do not have to do it by hand?

    
asked by Sergio Ramos 12.03.2018 в 15:04
source

1 answer

1

No need to use regular expressions .

To change the quotes , just a simple .replace('""', '\"\"') ; to remove comments, with .split("'")[0] ; and to collect all ":".join()

def sin_comentarios(s):
    return s.split("'")[0]

with open("fichero.txt") as f:
    lines = (sin_comentarios(l[:-1]) for l in f.readlines())
    res = ":".join(linea.replace('""', '\"\"') for linea in lines)

Note : unproven code

    
answered by 12.03.2018 в 15:50