Skip to content Skip to sidebar Skip to footer

How To Add New Column To Output File In Python?

I have this code written in Python. How can I add a new column 'qty' that contains the number '1' at each row? This is the file tripo: { '1.txt': [], '2.txt': [], '5.txt': [], '

Solution 1:

I don't see where you're writing the name column to your file. It could be done as:

with open("test.csv","wa") as f:
    f.write("name\tnum\n")
    for k, v in tripo.items():
        if v:
            f.write("1\t")
            f.write("{}\n".format(k.split('.')[0]))
            for s in v:
                f.write("1\t{}\n".format(s.split('.')[0]))

Post a Comment for "How To Add New Column To Output File In Python?"