[Python] Dealing with multivalue list item from csv [duplicate]

Discussão em 'Python' iniciado por Stack, Fevereiro 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a csv with a multivalued field.

    In parsing the records I have a list like:

    ["A|B|C", "1","2","3"]


    desired output is:

    ["A|B|C", "1","2","3", "A"]
    ["A|B|C", "1","2","3", "B"]
    ["A|B|C", "1","2","3", "C"]


    I am using code like:

    records = []

    def get_record(row):
    letter = row[0]
    letter_list = letter.split("|")
    for l in letter_list:
    record = row
    record.append(l)
    records.append(record)

    for row in csvreader:
    get_record(row)


    But the output is appending to the record each time and maintaining the previous list. Like so:

    ["A|B|C", "1","2","3", "A"]
    ["A|B|C", "1","2","3", "A", "B"]
    ["A|B|C", "1","2","3", "A", "B","C"]


    I don't understand my error. Usually, when I iterate through a list it iterates through the list one time. I am also resetting the value of record back to row each iteration.

    How can I get the desired output?

    Continue reading...

Compartilhe esta Página