[Python] Do something different on last row of a tuple

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I'm manually constructing a JSON so I want the last element to not have a , seperator before the ].

    This my code and the documentation says len() works for tuples but I can't get it to work. The data is a tuple constructed from a SQLite.fetchall() and it has been validated.

    for row in data:
    f.write("{\n")
    f.write(f"'PUT':'{row[1]}'\n")
    if row == len(data): # I also tried len(data)-1 as suggested by posts
    f.write("}\n")
    else:
    f.write("},\n")


    This code constructs the JSON but all elements including the last have the , separator.

    What is the best pythonic way to do this?

    Continue reading...

Compartilhe esta Página