How to add a new column to the beginning of CSV file?

后端 未结 2 1469
陌清茗
陌清茗 2020-12-10 18:06

I have one csv file in which I have 6 to 8 column.
Ex:

ID Test Description file-name module view path1 path2 
         


        
2条回答
  •  温柔的废话
    2020-12-10 18:53

    You can use the CSV module to read in your CSV file and write out an edited version with an appended column. Remember that adding a column is adding an extra entry to the end of each line.

    An example of outputting with the CSV module (http://docs.python.org/library/csv.html)

    >>> import csv
    >>> spamWriter = csv.writer(open('eggs.csv', 'wb'), delimiter=' ',
    ...                         quotechar='|', quoting=csv.QUOTE_MINIMAL)
    >>> spamWriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    >>> spamWriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])
    

提交回复
热议问题