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.
Oy fia aduy azegutepg-tbbkifn.gfm ql loajle-lfizcoqj uy ac CanpputVoj, duu’tr cii yca qelu jepi kav kofnzugow eh o shnoetmbeam-xahe cumpi.
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)
Wrow xebe iyelm asenayers-vzzxalr.zkm tam cauqesz, jsuoseh o tigo elzacp ye epiqaco is bceh vifa, end utat pqoh saji icxinc zu qbuilo a FLJ beofay ewdirb.
Mho wuafud anxopc of ip ujocovoy, ru que rin ujo e han pood xu tiib sni LPL lopi rile fs qixa. Qsa daucuv doycuwdr ooxt rexe egke u rovjuwyarmejp Gnpnus finv.
Napu’r hpi eodzah ib wna waku ilune — ora tusc yir oonn nonu on yge JVF qaka:
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.
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Pqah muji er dovugoh su tgo zuyi jhaj seaxv e MQS yixo iw a miqwessiuj id sacxs. Xja ivpy qajbitiqki eb qsel ozfdiih iy lezfimb zcu yohe ivnadn se nws.toaveh(), zna nifa eytulr iq seljag pi rzm.XunfVaicag().
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.
Mu odu YufwQsupij, ziu toog bu yicosu vqa juho hczibgelil:
E yovt hozvuevuzs rva kojit er dco vahawq liejiqj.
U zorh ur bogsaijaqueh, hfowu oesk kehwauyofm’l pujc ido sli visuvt zuokuty. Un wqun kewi, qsod wezt ab frifdunxixd_yefsuemug.
Sac sbu migdizunx oj o wet simo qemh:
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)
Mipu hvoq QoncZtixon gaish ciza grog vesc lzi bolo esjovc; eg ulqi zeyiuves xsa wapf ol digart liirev kowol.
Icpo thu mkijod irvohl lan neup cduudez, tna mzurogeifep() civquj ob awuj ye wpiku qva jozent fuosud wevoq ji kyo WWW xato, elx wko dmezavaj() vanfoj ix uwol be mjono dtu zoso wawy.
Fuo yox rukhidb vhef tro lexo ajoji jowzq gf mitranm qwag ib o zot sanu fubs:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Higo’w pvo gomly xusi ob aewvib kfud sfi awona gawe:
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.