Data Analytics Systems & Algorithms COMP 20036: Week 10
Data Analytics Systems & Algorithms COMP 20036: Week 10
Data Analytics Systems & Algorithms COMP 20036: Week 10
COMP 20036
Week 10
File / Data Handling
filename: gives name of the file that the file object has opened.
mode: attribute of a file object tells you which mode a file was
opened in.
File Modes in Python
f= open(“filename.txt","w+")
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
f.close()
\content\gdrive\My Drive\Colab Notebooks\
How to Append Data to a File
f=open(“filename.txt", "a+")
for i in range(2):
f.write("Appended line %d\r\n" % (i+1))
you could see a plus sign in the code, it indicates that it will
create a new file if it does not exist. But in our case we already
have the file, so we are not required to create a new file.
How to Read a File
f=open(“filename", "r")
if f.mode == 'r’:
contents =f.read()
def main():
f=open('filename.txt','w+')
#f=open(“filename.txt","a+")
for i in range(10):
f.write("This is line %d\r\n" % (i+1))
f.close()
#Open the file back and read the contents
f=open("filename.txt", "r")
if f.mode=='r':
contents=f.read()
print(contents)
#or, readlines reads the individual line into a list
fl=f.readlines()
for x in fl:
print(x)
if __name__=="__main__":
main()
Summary
• Data in the form of tables is also called CSV (comma separated values) -
literally "comma-separated values."
• This is a text format intended for the presentation of tabular data.
• Each line of the file is one line of the table.
• The values of individual columns are separated by a separator symbol - a
comma (,), a semicolon (;) or another symbol.
• CSV can be easily read and processed by Python.
Reading and Writing CSV Files in
Python using CSV Module & Pandas
• A CSV file is a type of plain text file that uses specific structuring to arrange
tabular data.
• CSV is a common format for data interchange as it's compact, simple and
general.
• Many online services allow its users to export tabular data from the website
into a CSV file.
• Files of CSV will open into Excel, and nearly all databases have a tool to allow
import from CSV file.
• The standard format is defined by rows and columns data.
• Moreover, each row is terminated by a newline to begin the next row.
• Also within the row, each column is separated by a comma.
Consider the following Table
Table Data
As you can see each row is a new line, and each column is separated with a
comma. This is an example of how a CSV file looks like.
Download CSV Data
Python CSV Module
Python provides a CSV module to handle CSV files. To read/write data, you need to loop through rows of the CSV.
You need to use the split method to get data from specified columns.
To read data from CSV files, you must use the reader function to generate a reader object.
The reader function is developed to take each row of the file and make a list of all columns.
How to Read a CSV as a Dictionary
reader = csv.DictReader(open("file2.csv"))
for raw in reader:
print(raw)
How to write CSV File
When you have a set of data that you would like to store in a CSV file you have to use writer() function. To
iterate the data over the rows(lines), you have to use the writerow() function.
Reading CSV Files with Pandas
CSV files are widely used in software applications because they are easy to
read and manage, and their small size makes them relatively fast for
processing and transmission.
The csv module provides various functions and classes which allow you to
read and write easily. You can look at the official Python documentation
and find some more interesting tips and modules. CSV is the best way for
saving, viewing, and sending data. Actually, it isn't so hard to learn as it
seems at the beginning. But with a little practice, you'll master it.
Thank You