CSV
💡 Code
import csv
with open('./resource/test1.csv', 'r') as f:
read_ = csv.reader(f)
print(read_)
print(type(read_))
print(dir(read_))
print()
for c in read_:
print(' : '.join(c))
with open('./resource/test2.csv', 'r') as f:
read_2= csv.reader(f, delimiter='|')
for c in read_2:
print(c)
with open('./resource/test1.csv', 'r') as f:
read_3 = csv.DictReader(f)
print(read_3)
print(type(read_3))
print(dir(read_3))
print()
for c in read_3:
for k,v in c.items():
print(k,v)
print('----------------')
w = [[1,2,3],[4,5,6],[7,8,9],[10,11,12],[13,14,15],[16,17,18],[19,20,21]]
with open('./resource/write1.csv', 'w',encoding='utf-8') as f:
print(dir(csv))
wt = csv.writer(f)
print(type(wt))
for v in w:
wt.writerow(v)
with open('./resource/write2.csv', 'w',encoding='utf-8') as f:
fields = ['One', 'Two', 'Three']
wt = csv.DictWriter(f,fieldnames=fields)
wt.writeheader()
for v in w:
wt.writerow({'One': v[0], 'Two': v[1],'Three':v[2]})