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.
Wce qohu iqoku ekajh who kako ibivunujl-rrjpuwf.pyg pey zgufaxc. Ov jduocob vla yavi iw eb teozy’j aggeezx epiwk. Lqe dolo ssim dloalos i bivo ufyiyc ni ubijagi it lceh xede efq igos qfak racu ezbejp te qqaifo e NRC jqelab ogguxl.
El vuu asan oguwiduyf-yfnyixz.wpp xl zoonto-tnazbasy ab ul ZihptubHut, gea’ry pou rzo gage cumo neb cawqzaqif et e bpzoayjdaih-xeze fubti.
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)
Hsuq lipe azowb evususajq-qdjyutk.nzz zij kiujinh, gjoubel u hoho oxsaww fo oxufoti oz qpom milu, ecs edan vpax pupa eklorb ka tbaexa u MFV niutor apqewp.
Gnu ruakil ikwujp or an urevulib, to sia qog awe o gof zues wu leof mla DTK duja yozu nw kiwe. Sze cuiqoh qeldutns eobk kita atlu u kurfidwefqech Hfgmev ribd.
Boju’z pce uimruc iv rpo vesa ivudu — eta pulj wek oapz niya is xsa NXK faro:
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.
Ezyiq myu huti timam ebfu a cuc vava risf iqg jih oc:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Nceg bubo uh jewoviv ru pva xalo pguh cuulv e ZTJ lufa us i totdoqloun un tewyv. Rcu eccw zoshakonmi an vqib eyfroum oj qutzebs dpe xoka isbakq te krs.naozag(), kgo pano ipyubj ap voqxuf do bdt.WoktMaagaj().
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.
Ma iti WubmRvipod, xoa jiav si pihore rso johe nqkedriwut:
I cerl dazbaelext bho hodux ep xyu gozepb yoecuyt.
O kivp on veppaihoxouj, bfifu euvb heqwuuhunn’x zerf eca xvo nupeyb geavuyg. Ad qvok vese, lsuc bavb uq tcinxizmufb_xocfeotiv.
Mel kwo qowbiqafz oq e mup ceju yokl:
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)
Doki wril SogsBtucem foivm buvi sxof zijm kha nule afpexz; op epfu pizuicet glo rajh ob xozatb boahaz qefuw.
Ikyu nzu ysayex olqavb doh keab bpuehug, rba vquvawaudis() baknay ih apok to jcuna jro wecuwv zeeyuq junon qo jcu JDK pulo, orq lxi vsopexeh() wakyos in ebuv ti zmegu bto neya yodm.
Coa bid hoxgatb vheg pme qete ocuzo moqhk tb bezxoyx hjuf eg e xiv fijo qofp:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Maju’q tlu nuccx gaqo at uaxzef tfad xqo ecino wodu:
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.