read_csv example

This commit is contained in:
Mert Gör ☭ 2023-08-26 15:04:33 +03:00
parent a9fe445fa9
commit ddacee5bcd
No known key found for this signature in database
GPG Key ID: 2100A876D55B39B9
2 changed files with 27 additions and 0 deletions

4
python-temel/people.csv Normal file
View File

@ -0,0 +1,4 @@
isim,no,doğum yeri,oran
ali,123,eskişehir,4.2
mehmet,256,eskişehir,5.8
sacit,452,urfa,7.2
1 isim no doğum yeri oran
2 ali 123 eskişehir 4.2
3 mehmet 256 eskişehir 5.8
4 sacit 452 urfa 7.2

23
python-temel/read_csv.py Normal file
View File

@ -0,0 +1,23 @@
def read_csv(path, converters=None, header=False):
with open(path) as f:
if header:
h = f.readline()[:-1].split(',')
l = []
for line in f:
a = line[:-1].split(',')
if a[0] == '':
continue
if converters:
for key, value in converters.items():
a[key] = value(a[key])
l.append(a)
if header:
return l, h
return l
l, h = read_csv('people.csv', None, True)
print(l)
print(h)