Adding the header to a csv file

后端 未结 4 1260
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 20:21

I have a csv file with the dimensions 100*512 , I want to process it further in spark. The problem with the file is that it doesn\'t contain header i.e

4条回答
  •  独厮守ぢ
    2021-01-26 20:31

    you can use it :

        import csv
    
        with open('names.csv', 'w') as csvfile:
            fieldnames = ['first_name', 'last_name']
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    
            writer.writeheader()
            writer.writerow({'first_name': 'Baked', 'last_name': 'Beans'})
            writer.writerow({'first_name': 'Lovely', 'last_name': 'Spam'})
            writer.writerow({'first_name': 'Wonderful', 'last_name': 'Spam'})
    

提交回复
热议问题