In: Computer Science
How can I use Python without Pandas to create a new.csv file with the headers "c1, c2, c3, c4, c5, c6" and the first line of data "8, 5, -9, 7, 2.1, 1.7" from the original.csv file?
Answer
Here is your answer,if you have any doubt please comment, i am here to help you,
Here is the code ,
import csv #import csv module
import numpy as np
with open('original.csv', newline='') as f: #reading original csv file and getting fist line.
reader = csv.reader(f)
row1 = next(reader)
# data rows of csv file
row1=np.reshape(row1, (1, 6)) #change based on your column, first line
# field names
fields = ['c1', 'c2', 'c3', 'c4', 'c5', 'c6']
# name of csv file
filename = "new.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv writer object
csvw = csv.writer(csvfile)
# writing the fields
csvw.writerow(fields)
# writing the data rows
csvw.writerows(row1)
output
Any doubt please comment
Thanks in advance