0

Hola tengo un print con 3 respuestas y estas las quiero poner cada una en una variable alguien sabe como le puedo hacer?? Estoy colocando un split pero me da error

print("end", finalLayout)

response = finalLayout
response.split(',')
print(response)

RESPUESTA DE PRINT

end('new-vdttxya0.docx',{
    'A': 'PAGARE',
    'B': 'http://expediente.com/id+11',
    'DEL': '',
    'STS': 'Valid',
    'MSG': 'View current status and electronic document when online'
}, {
'api_version': '2.2',
'scheme': 'C02-HDC-S-B64PNG',
'status': 'C'})

LO QUE NECESITO

layout = 'new-vdttxya0.docx'
content = {
        'A': 'PAGARE',
        'B': 'http://expediente.com/id+11',
        'DEL': '',
        'STS': 'Valid',
        'MSG': 'View current status and electronic document when online'
    }
content2 = {
    'api_version': '2.2',
    'scheme': 'C02-HDC-S-B64PNG',
    'status': 'C'}

1 respuesta 1

0

Está es una solución "primitiva", parseando el texto a mano.

def parse(texto):
    breaks = [] # indices del texto en donde se detecta un ", {"
    prev = ''   # caracter previo, distinto de espacio
    for i in range(len(texto)):
        if texto[i] == '{' and prev == ',':
            breaks.append(i)
        if not texto[i].isspace():
            prev = texto[i]
    breaks.append(i + 1)

    partes = []
    inicio = 0
    for fin in breaks:
        partes.append(texto[inicio:fin])
        inicio = fin

    return partes

La idea es que el texto consiste de diccionarios separados por comas. La función detecta esos puntos (", {"), guardando en la lista breaks el indice del "{".

La función luego divide el texto en trozos según los indices en breaks.

Demo

layout = \
"""'new-vdttxya0.docx',{
    'A': 'PAGARE',
    'B': 'http://expediente.com/id+11',
    'DEL': '',
    'STS': 'Valid',
    'MSG': 'View current status and electronic document when online'
}, {
'api_version': '2.2',
'scheme': 'C02-HDC-S-B64PNG',
'status': 'C'}"""

layout, content, content2 = parse(layout)
print(f"layout = {layout}")
print(f"content = {content}")
print(f"content2 = {content2}")

produce:

layout = 'new-vdttxya0.docx',
content = {
    'A': 'PAGARE',
    'B': 'http://expediente.com/id+11',
    'DEL': '',
    'STS': 'Valid',
    'MSG': 'View current status and electronic document when online'
}, 
content2 = {
'api_version': '2.2',
'scheme': 'C02-HDC-S-B64PNG',
'status': 'C'}

Process finished with exit code 0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.