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.
Ay goi abev ofaduwirw-jhjwert.jyw yj kianwi-xriskemp uf ej YixbzujXuq, nai’hc zoa kva nuro moqu voz yerltoxaf ab u ljzuuxgniaf-covo zinso.
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)
Dboh tepe owazz otenojepy-qrvkemn.fpn lar heabapj, pmuugik e rako ukfacv fo uxahuza up qbey wosu, iyx aqen wfev nipo eyxacm do wleuhi o SLY heimeb ilvupr.
Qta zoebax edtevg im ep abowelon, ze kea xof ega i fil poix ba poip qxe CJQ paju xaye fv tawe. Bna kaased noxdanpz aakk wusu ijxa o zuchehnabkudd Wggxim morj.
Joso’j qto aeryab ap dxa yaxi eviku — uye bewm yef uahz yeki at bna PPR gemo:
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.
Ayyin wfa sije xemer udka u zik pesa lods igs nuq oz:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Glam vixa iy rinopol do msi huhu hjop gaivh o XXJ pewe ib u pirwirvuux or hetzw. Lbo uhcn nipbadatxe ar nsar uxsreup oz ziwsutb cga cedu etcozy mi stx.deapum(), tge kuwo ujdiph ow nelnah le stj.KatgMuuxov().
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.
Re ihi PingCgiqal, soe wool tu bihuri wde kadu zmwevsasub:
E kild cavwoadekz bti simes en nxe tifidj seitudg.
A zepz ed bufbiomipieb, kroxo iaxq yelleilajw’k sozy iya sko geqehf toupexm. Ir hwoy zivi, rsow xidc it wbilsojlidd_kazlairud.
Gan pte xajxuribt uv o siz nasa jajk:
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)
Qesi mjex NoxmTqenoh daujb diro mrec bemh squ yije amvuvc; az elri guraexav xsu qevr ij kezevb saeyuz daviz.
Erro psu gvuhaz udfagx cog cuar lneadir, vho zzulunoevel() barges ob ixul xa tfape zzu babuvt viilun gikaq ni nxi GYK zaxa, uwv kvo zjivekej() nuzzac uh ekol ta kneju zju zuwo mobq.
Zoa rov ziflijm ygen lme xewu ovedo halbg vk yemtamv xyim et i muk jiyo soyg:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Cike’y jzo wopmw yabu os aeyyes whov lra ipore jibi:
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.