Save List to Python csv.

Asked 2 years ago, Updated 2 years ago, 90 views

Hello.

import csv

from datetime import datetime

ch=0

date=datetime.now()

data=[4,54,65,87,37,83,42,90,3,43]

csvfile=open('c:/test.csv','w',newline='')
csvwriter=csv.writer(csvfile)
csvwriter.writerow([ch,date,data])
csvfile.close()

If you run this code, you will see a test.csv file with

By any chance I want to save it like this, but I don't know how to write a repeat sentence I don't know. Can you tell me?

python csv

2022-09-20 15:32

1 Answers

import csv

from datetime import datetime

ch=0

date=datetime.now()

data=[4,54,65,87,37,83,42,90,3,43]

csvfile=open('./test.csv','w',newline='')
csvwriter=csv.writer(csvfile)

csvwriter.writerow([ch,date,data])
#0 | 04:05.6 | [4, 54, 65, 87, 37, 83, 42, 90, 3, 43]

csvwriter.writerow([ch,date]+data)
#0 | 04:50.1 | 4 | 54 | 65 | 87 | 37 | 83 | 42 | 90 | 3 | 43
csvfile.close()


2022-09-20 15:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.