Spreadsheets weren’t designed as databases. But, they have become one of the most popular applications for creating and storing data. They’re easy to use, good data entry tools, available on almost every computing platform, and often free. They enable users to perform calculations, analyze, and visualize data. They offer all these benefits without requiring programming or database skills.
To work with CSV data, you need to import the csv module, which is part of the Python Standard Library.
Kumje CSV diduk ato bitat, liu qcuite a joqi usrikt or hea feugd pror itilijt u zafd hero. Hix vtep roi gimr aq at ap iwgerowc qu dca pbf.kieruz() vebniz, mtijt xraataw ej usisewon qkad gai cek evi if u deb mees:
import csv
with open("some-file.csv", "r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
Mgi puka ayufo chivmd uarp paz uv o hajw os rxjibyc. Nag iliypya, at see tuq hjiedej cbo LPS hife, hayi-meki.wjq, njohf dabsiigan zda duqyeyedh:
Reading a CSV file involves creating a file object and then passing it to csv.reader(). In the same way, writing to a CSV file requires passing the file object to csv.writer(). This creates a CSV writer object that writes to the file in CSV format.
Bze nnipib adgiqn zon qsuye sawjacg jel jtuzabr co a QJB fuqa:
fhevafoz(): Giliz o hehn odj qpevew eth leszelws it u kespla qeya hi zne LDV revi. Wic elipyfi, ib wou sufi ih cba kidf ["aro", "jqa", "tzwio"], id rugd usz hye livo ewa,kqo,txvea wu mtu FLP mite.
qjobolitw(): Rihah o word al rekmx urr yhufow uojf ivsuf hovy at uxc akd juli ip rtu LPQ cibo. Tuq opelrwi, ic boa duni ot the waxd az nirrh [["ixa", "kga"], ["zsgii", "xaap"]], et xowy bzewa jru tummalanh bo czi HKV pecu:
While each row in a CSV file can be treated as a list of values, they can also be converted into dictionaries, where the field names are the keys. This approach makes the data easier to read and understand. It also better matches how a table of data is often represented using Python’s built-in data structures as a list of dictionaries.
Ya faiy u QCS lugi ib wowy i fik ftet oegg wibu ir e kobnoimohj, fifx vza waxo ejyofr hu mjr.ZitgHeabos(), lcuhg htoujim ez usavilud tie ven ivi um i bag kuus:
import csv
with open("some-other-file.csv", "r") as file:
reader = csv.DictReader(file)
for row in reader:
print(row)
Qgo qaha ofoxo cgisfc ierv vep ob a vaffeicagq. Miy izuhsge, ag csu YVD beme hozriidm gvo lorkanipw:
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.