Answers for "csv writer header python"

38

csv python write

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'})
Posted by: Guest on March-10-2020
1

how to add header in csv file in python

#header in csv file
import pandas as pd
import csv
df = pd.read_csv("file.csv", header=None)
df.to_csv("file.csv", header=["SEQUENCE"], index=False)#header added
df = pd.read_csv("file.csv")
df
Posted by: Guest on October-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language