import csv
with open("test.csv","w",newline="") as f:
writer = csv.writer(f)
writer.writerow(["symbol","date","close"])
writer.writerow(["rb2101","20200907","3736"])
writer.writerow(["rb2101","20200908","3719"])
writer.writerow(["rb2101","20200909","3758"])
f = open("test.csv","w",newline="")
writer = csv.writer(f)
writer.writerow(["symbol","date","close"])
writer.writerow(["rb2101","20200907","3736"])
writer.writerow(["rb2101","20200908","3719"])
writer.writerow(["rb2101","20200909","3758"])
f.close()
with open("test2.csv","w",newline="") as f:
writer = csv.DictWriter(f,fieldnames=["symbol","date","close"])
writer.writeheader()
data = [
{
"symbol":"rb2101",
"date":"20200907",
"close":"3736",
},
{
"symbol":"rb2101",
"date":"20200908",
"close":"3719",
},
{
"symbol":"rb2101",
"date":"20200909",
"close":"3758" ,
}
]
for d in data:
writer.writerow(d)
with open("test2.csv","r") as f:
reader = csv.DictReader(f)
for row in reader:
print(row)
{'symbol': 'rb2101', 'date': '20200907', 'close': '3736'}
{'symbol': 'rb2101', 'date': '20200908', 'close': '3719'}
{'symbol': 'rb2101', 'date': '20200909', 'close': '3758'}
with open("test2.csv","r") as f:
reader = csv.reader(f)
for row in reader:
print(row)
['symbol', 'date', 'close']
['rb2101', '20200907', '3736']
['rb2101', '20200908', '3719']
['rb2101', '20200909', '3758']