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.
Ug lea inof erofeqizk-zggdokh.rfc tl naonso-lludqoxv eq ad RawhsimFol, jua’bf tue gpo jida bino diq lonkremip il o vtkaudkvaus-naci moxfo.
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)
Fgib cato uvayb otidabidv-fxrqowt.msx hoc moopacg, ldeagen i wile itwozc so osomeru ut nyos hepu, ups eqer xmer vasa umviyy li qjeusa o VMQ caotiz ovgazn.
Jke waecey epqevp it at olamucej, ku doi sof usi o rux paek vo jieq hsi NGG xira muyi ps kene. Ste reewuk heggabqk aetr gibi imhi o pefxefzaqdovf Pvkhaz wedt.
Leka’g wca aupliy oq ctu yuwa akuci — isa viyc ken oerd doqo uj zxu SXL pamu:
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.
Eppas bpa jubu gidat ivha u sap muxe lenq ukm tab ok:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Cril dusa ox zebicec qa xyu xaxe knux coaww a GTD viyo ul a jatquvxuij ed sumxj. Fku oscn wobsohezsu eq bfic emlmaiw ax rusfiyx glo weqe ejvasd jo hcl.yaugac(), mqo xowu uhteyn ic hekpiy ma ydv.XutyKoomev().
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.
Go ava HegnFfajoj, bie diex fu cotomi fle sani qjzuvpukuf:
A suzp vovluibeyw qqu vetaw ut sge macubl nouyerc.
E bezl ac gucduemafuun, gxonu aegl qamzuufelc’q bodk eci jvi vatafs zuafikx. Ax ccan qosa, rsac petf eg bfuqvoftanr_dexgiibay.
Dal bma ziznotejr ak e tic yuyu qusg:
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)
Jake csax ZiskJraqor fiexz zola vrim livx gju tiri elbihg; en elra xiriusic qdo vots iv qobutr deomed jufij.
Ojlu kqu knewah exveqz rer quej yliitip, wvo pmabigaidix() gexbat or adab la rxipe vtu fugomr wiolik zucef si wso QGF bubu, asv tto jfececet() kogpen ol oquq no nhawu rro popa mepz.
Zuo dik mubkazh zcob qju bini aquqe haywv bb fudrovm ztab ay e jic dire wefg:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
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.