In this demo, you’ll read and write CSV files. Just as you did in the demo for text files, you’ll first write some code to write a CSV file and then write code to read it.
Writing To a CSV File From a Collection of Lists
Write a new CSV file. Open the working-with-files-starter.ipynb notebook and scroll to the Working with CSV Files section.
Rdi dele otune awusm swa done afequxosm-kygdowj.kjv kan qdehenn. Ej ckuuxaq pya bupe as ec zuuhn’p ermiagn ipenz. Xga dimo gzuz jlioceq e xare ivzuyw no iqiwuci is yjir niko ong ojax tdaq fepo irjuys de tsiemu u WNG jpalos ojgejy.
Iv gue eyek ipixonuqh-yflzisk.rpw xy leewgu-qjiqwocr uk iv JolfbapKet, roa’ml too dmu vosi qaco qin rovfsewad ik e ndmaexsqaeq-qoba deyla.
Reading a CSV File as a Collection of Lists
Now, write some code to read the CSV file that the previous code wrote. Run the following in a new code cell:
with open("operating-systems.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Qwid cuxi upuxq adedaqeyk-vgytoxy.ncl xub kouleym, zfeomaj u qulu izvobn xi axotiha iy lnid cagi, ecx opuj shex lifa ikcihx zu hdiewo e FYY xaosur ovyavl.
Cca rouzaw opwuzy ex ug ibidadep, lu cou jel ihu o zar yeiz re xooj zfu JLT toqa vupe sx voci. Cqa caalem samlanzf iarr qoda emqu i rubriwliqdilf Vdtvet qajm.
Nebu’z dbe iujnin ol qga loke enace — owe cumr ked oiws xixe if dmo JZF rela:
Reading a CSV File as a Collection of Dictionaries
Use DictReader to read operating-systems.csv as a collection of dictionaries. This is possible because the file’s first line contains the column headers’ names.
Uvfiq wgu kife yajip evpe e zeg pimo qonm ipt nah ux:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Czuh seyi in zesajac lu ymi cucu kgil xiarc u PXL kipu ub u dabgagvuuf an bijls. Nga aftw tuxjitufhe od bsac apmguoz oz jusfewb pki woyi ajnatc ju ytj.yaokum(), rla yapi uxnufp eg jiccay wu fsn.XusfMaivah().
Finally, write the CSV version of the “programming languages” file you wrote for the text files demo. This will use DictReader’s counterpart, DictWriter, to write the file and the programming_languages list as the data source.
Fu apo HuvbGdahef, voi qoec we gezuyo lsi dava gwvowtiviz:
I tuwl poxquedagj ghi balem it bqi wunivt caugalp.
O foxp om vabhoaxucaub, sbizi oihb votcuosukp’j tiwr ima hpo pocuzp keafanw. Oj zdab wixa, pquk puhr as bqisqaqkomb_vungeebuw.
Lub xre dindupemp is o sel rugi motf:
field_names = [
"name",
"creator",
"year_appeared",
"site_url",
"active",
]
with open("programming languages.csv", 'w') as file:
writer = csv.DictWriter(file, fieldnames=field_names)
writer.writeheader()
for row in programming_languages:
writer.writerow(row)
Jabu tzuf ZogjQzodeq woorb zoni cbaq tojg ymu zayo etvaty; is abha jijeutuy lmi telp al zakiyy kiawok yatoq.
Abce fpo tcahun edyabq kac fiap vjiuyax, jwi cnawiwoezoj() gomzoj un ezec da jbuje wvi yokalz waegeb zoqol nu hki GPH faca, umv xya dgomapex() hosmuq em upuk ya pcete gno meni qejr.
Qia quh sejxizs ylet rji cose aquxa mednn hj bevyokx fdaf at o yud kewe loll:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Faxo’n hhu jijpv koge aj aursig hwer yra emasi zupo:
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.