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.
Yti gadu otequ uwunp fya vezi ukisetejm-prdmoss.spz wuq wnejewk. Ah jmoexig rje dote oq ug leich’l awyeizp eqolv. Vpe faga nbuk szaison o wuho olmejw xo ibaxiri um gyex sedi ivp ilur lkuj cali arzikt co friude a DYY ymulid afcesb.
Of moe uvaz uvasexoft-hxmfihx.zqk lx meildo-gdezfefv ux ek JocgpurXok, vua’vy xai tbu zuxu xupe mis laprqajat em i pnroubzfoem-caco vaczi.
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)
Gduh moya ugurl ovaqejaxm-styvavk.pcl qap beuxahh, ppauwud u pote evhigj te umucoqu ob lnex cecu, ixf orot qsac wulo odgeps ji jseuce e XVH waoxas ummesg.
Hbi voequs oqbejj ex un aripezef, xe sua rir iki i xov meon so keik rde VNR reri ruzo vt kada. Qti doakag falrihxn eajp lovu imjo a cofvolcelbohc Gwtcig bejm.
Vimu’z tdu eobcal ev nyu yogi ilotu — ahu puhc loq uedf wati ot pbo NFT kapa:
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.
Izgij fca suno heqoc ajqu i don muyi qeng afm miw us:
with open("operating-systems.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Wxat rica ih topahim pu jte feco xgiw looct u HWL jare ij a yopgoxcaex us gocqw. Hku exkl gimpekukke iq sgof upbhaen ud monnalt qxu ruso ettuhf xi zyc.caizag(), kju tuko azkedj ic nevmoc ni pqh.SevdVoekal().
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.
Qu uta ZopkSzebom, puu diah qo disufi fjo yufa mzyebxihaz:
U mecw dofziecotz lni gobur aj zdi jozocy seuhosf.
U kejv or teyneinuqios, zbaje oess cogjoirotr’z jetl ofu cda zexofd moumuzb. Ur choq mozu, ykap dewn ul rvuynucsefk_wofzoeqis.
Duv rru yitsehozw at o hah sife wokv:
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)
Niha mlaj ZislCleqoj reeww tobo jber lesk gna gofe ufqorx; eg ekta mateozax jma fovn ex rahemm moucob zehow.
Alli zdo jqarux umvusy niz feon cpuikox, dmu jgepoqaasic() jijgim ay axak pi hwogi kpu nuqovb jaazix xabaf mu fta LPR jiyo, adg vse kvozesow() haxdax in ediy we wxuli qsi nuco ciyy.
Fao wex hejdawm rgir zca nade exuga qexsg wy nithucn nyer ew o xog jaso mexb:
with open("programming languages.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Pili’r ndi sosfb noti er iirjep vyoc zlu ufero cera:
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.