Python Projects for Beginners a ten-Week Bootcamp Approach to Python Programming



Download 2,61 Mb.
bet100/200
Sana20.06.2022
Hajmi2,61 Mb.
#681748
1   ...   96   97   98   99   100   101   102   103   ...   200
Bog'liq
Python Projects for Beginners A Ten Week Bootcamp Approach to Python

Writing to CSV Files


CSV files work with data by separating a comma between each cell. This is known as a tabular data structure. To get started working with them, Python has a default library called “csv.” We’ll need to import that in order to work with them. After importing this library, we’ll use the second method of opening files using the “with” keyword. This concept works like a while loop, so that while the file is open, we can work with it, and once the block of code is done running, it closes the file automatically for us. Let’s check out the example:

1| # opening/creating and writing to a csv file
2| import csv 3| with open("test.csv", mode="w", newline="") as f:
4| writer = csv.writer(f, delimiter=",")
5| writer.writerow( ["Name", "City"] )
6| writer.writerow( ["Craig Lou", "Taiwan"] )


Go ahead and run the cell. Let’s walk through this line by line. We import the CSV library on line 2. Then we open the file in write mode as the variable f. We’ve also set the newline parameter to an empty string so that it doesn’t create empty lines between rows. On line 4, we create a writer variable that allows us to write to the CSV file. The last two lines write a couple lines of data to the CSV file. Once the block is complete, the file closes automatically, and we’re done. Go ahead and check out the file; you’ll see the new data output. Remember that write mode will always overwrite any data that was in the file previously.

Reading from CSV Files


In order to read the data from the CSV file we just created, we can simply set the mode to read:

1| # reading from csv files 2| with open("test.csv", mode="r") as f:
3| reader = csv.reader(f, delimiter=",") 4| for row in reader:
5| print(row)
ChapTer 6 DaTa ColleCTions anD Files
Go ahead and run the cell. You’ll notice that it outputs each row as a list with two items inside. We opened the file in read mode as the variable f. We then create a reader object through the CSV library which reads the contents in the file for us. Then we loop over the reader variable and print out each piece of data.
Note objects will be covered in a later week.

Download 2,61 Mb.

Do'stlaringiz bilan baham:
1   ...   96   97   98   99   100   101   102   103   ...   200




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish