Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
19 views39 pages

Unit-4 Files and Data Bases Notes

This document covers file and database management in Python, detailing file I/O operations, directory management, and the use of modules like os and shutil for file handling. It explains how to open, read, write, and close files, as well as manage directories, including creating, renaming, and deleting them. Additionally, it discusses the advantages and disadvantages of file handling in Python and provides code examples for various operations.

Uploaded by

rameshapositive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views39 pages

Unit-4 Files and Data Bases Notes

This document covers file and database management in Python, detailing file I/O operations, directory management, and the use of modules like os and shutil for file handling. It explains how to open, read, write, and close files, as well as manage directories, including creating, renaming, and deleting them. Additionally, it discusses the advantages and disadvantages of file handling in Python and provides code examples for various operations.

Uploaded by

rameshapositive
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 39

UNIT-IV FILES AND DATA BASES

Syllabus
FILES AND DATA BASES
File I/O operations – Directory Operations – Reading and Writing in Structured Files: CSV and
JSON – Data manipulation using MySQL.

File Handling in Python


File handling refers to the process of performing operations on a file such as creating,
opening, reading, writing and closing it, through a programming interface. It involves managing
the data flow between the program and the file system on the storage device, ensuring that data is
handled safely and efficiently.
Opening a File in Python
To open a file we can use open() function, which requires file path and mode as
arguments:
# Open the file and read its contents
with open('hello.txt', 'r') as file:
This code opens file named hello.txt.
File Modes in Python
When opening a file, we must specify the mode we want to which specifies what we want to do
with the file. Here’s a table of the different modes available:

Mod
e Description Behavior

Opens the file for reading. File must exist; otherwise,


Read-only mode.
r it raises an error.

Opens the file for reading binary data. File must exist;
Read-only in binary mode.
rb otherwise, it raises an error.

Opens the file for both reading and writing. File must
Read and write mode.
r+ exist; otherwise, it raises an error.

Opens the file for both reading and writing binary


Read and write in binary mode.
rb+ data. File must exist; otherwise, it raises an error.

w Write mode. Opens the file for writing. Creates a new file or
Mod
e Description Behavior

truncates the existing file.

Opens the file for writing binary data. Creates a new


Write in binary mode.
wb file or truncates the existing file.

Opens the file for both writing and reading. Creates a


Write and read mode.
w+ new file or truncates the existing file.

Opens the file for both writing and reading binary


Write and read in binary mode.
wb+ data. Creates a new file or truncates the existing file.

Opens the file for appending data. Creates a new file if


Append mode.
a it doesn’t exist.

Opens the file for appending binary data. Creates a


Append in binary mode.
ab new file if it doesn’t exist.

Opens the file for appending and reading. Creates a


Append and read mode.
a+ new file if it doesn’t exist.

Append and read in binary Opens the file for appending and reading binary data.
ab+ mode. Creates a new file if it doesn’t exist.

Creates a new file. Raises an error if the file already


Exclusive creation mode.
x exists.

Exclusive creation in binary Creates a new binary file. Raises an error if the file
xb mode. already exists.

Exclusive creation with read and Creates a new file for reading and writing. Raises an
x+ write mode. error if the file exists.

xb+ Exclusive creation with read and Creates a new binary file for reading and writing.
Mod
e Description Behavior

write in binary mode. Raises an error if the file exists.

Functions:
 Reading a File
 Writing to a File
 Closing a File
 Handling Exceptions When Closing a File
Reading a File
Reading a file can be achieved by file.read() which reads the entire content of the file.
After reading the file we can close the file using file.close() which closes the file after reading it,
which is necessary to free up system resources.
Example: Reading a File in Read Mode (r)
file = open("filename.txt", "r")
content = file.read()
print(content)
file.close()
Output:
Hello world
File content
123 456
Reading a File in Binary Mode (rb)
file = open("filename.txt", "rb")
content = file.read()
print(content)
file.close()
Output:
b'Hello world\r\nfilecontent\r\n123 456'
Writing to a File
Writing to a file is done using file.write() which writes the specified string to the file. If the file
exists, its content is erased. If it doesn’t exist, a new file is created.
Example: Writing to a File in Write Mode (w)
file = open("filename.txt", "w")
file.write("Hello, World!")
file.close()

Writing to a File in Append Mode (a)


It is done using file.write() which adds the specified string to the end of the file without erasing its
existing content.
Example: For this example, we will use the Python file created in the previous example.
# Python code to illustrate append() mode
file = open('filename.txt', 'a')
file.write("This will add this line")
file.close()
Closing a File
Closing a file is essential to ensure that all resources used by the file are properly
released. file.close() method closes the file and ensures that any changes made to the file are
saved.
file = open("filename.txt", "r")
# Perform file operations
file.close()
Using with Statement
with statement is used for resource management. It ensures that file is properly closed after its
suite finishes, even if an exception is raised. with open() as method automatically handles closing
the file once the block of code is exited, even if an error occurs. This reduces the risk of file
corruption and resource leakage.
with open("filename.txt", "r") as file:
content = file.read()
print(content)

Output:
Hello, World!
Appended text.
Handling Exceptions When Closing a File
It’s important to handle exceptions to ensure that files are closed properly, even if an error occurs
during file operations.
try:
file = open("filename.txt", "r")
content = file.read()
print(content)
finally:
file.close()
Output:
Hello, World!
Appended text.
Advantages of File Handling in Python
 Versatility : File handling in Python allows us to perform a wide range of operations, such
as creating, reading, writing, appending, renaming and deleting files.
 Flexibility : File handling in Python is highly flexible, as it allows us to work with
different file types (e.g. text files, binary files, CSV files , etc.) and to perform different
operations on files (e.g. read, write, append, etc.).
 User – friendly : Python provides a user-friendly interface for file handling, making it
easy to create, read and manipulate files.
 Cross-platform : Python file-handling functions work across different platforms (e.g.
Windows, Mac, Linux), allowing for seamless integration and compatibility.
Disadvantages of File Handling in Python
 Error-prone: File handling operations in Python can be prone to errors, especially if the
code is not carefully written or if there are issues with the file system (e.g. file
permissions, file locks, etc.).
 Security risks : File handling in Python can also pose security risks, especially if the
program accepts user input that can be used to access or modify sensitive files on the
system.
 Complexity : File handling in Python can be complex, especially when working with
more advanced file formats or operations. Careful attention must be paid to the code to
ensure that files are handled properly and securely.
 Performance : File handling operations in Python can be slower than other programming
languages, especially when dealing with large files or performing complex operations.
File I/O operations:
In Python, "file I/O" (Input/Output) refers to the process of reading data from a file on
your computer and writing data to a file, essentially allowing your program to interact with
external data storage; this is done primarily using the open() function, where you specify
the file name and the mode (like reading 'r', writing 'w', or appending 'a') to access the file,
and then use methods like read(), write(), and readline() to manipulate the data within the
file.
 Opening a file:
 Use the open(filename, mode) function to open a file.
 filename: The path to the file you want to access.
 mode:
 'r': Read mode (default)
 'w': Write mode (overwrites existing content)
 'a': Append mode (adds new data to the end)
 'r+' : Read and write mode
 'w+' : Write and read mode
Reading from a file:
 file_object.read(): Reads the entire file content as a string
 file_object.readline(): Reads one line from the file at a time
 file_object.readlines(): Reads all lines from the file as a list
Writing to a file:
 file_object.write(data): Writes a string of data to the file
 file_object.writelines(list_of_strings): Writes a list of strings to the file
Closing a file:
 file_object.close(): Always close a file when you are done using it to prevent data
loss and release system resources.
 Error handling: Use try-except blocks to handle potential errors like file not found or
permission issues.
Example:
# Read data from a file
with open("data.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line.strip())
# Write data to a file with open("output.txt", "w") as file:
file.write("This is some data written to the file")
Python Directory Management
Python Directory Management refers to handling and interacting with directories (folders)
on a filesystem using Python. It includes creating, deleting, navigating and listing directory
contents programmatically. Python provides built-in modules like os and os.path and the
newer pathlib module, for this purpose.

os and os.path module


os module provides functions to interact with the operating system, such as managing
directories, files, processes and environment variables. It is cross-platform and adapts to
the underlying OS (e.g., Windows, Linux, macOS).
Creating new directory
 os.mkdir(path): Creates a single directory at the specified path. Raises an error if the
directory already exists.
 os.makedirs(path): Creates a directory and any necessary parent directories. Useful for
nested directory creation.
importos
# Create a single directory
os.mkdir("my_directory")
# Create nested directories
os.makedirs("parent_directory/child_directory")
Getting Current Working Directory (CWD)
 os.getcwd(): Returns the absolute path of the current working directory where the script is
running. Useful for determining where your script operates, especially when working with
relative paths. It returns a string representing the directory path.
importos
print("String format :", os.getcwd())
print("Byte string format :", os.getcwdb())
Output
String format : /home/guest/sandbox
Byte string format : b'/home/guest/sandbox'
Renaming a directory:
 os.rename(src, dst): Renames a directory (or file) from src to dst. The source (src) must
exist and the destination (dst) should not already exist.
For example, consider there is a file named ‘file1.txt’ in current working directory. Now
to just rename it :
os.rename("my_directory", "renamed_directory")
If renaming and moving the file to some other directory is required, then the code snippet
should be:
importos
os.renames('my_directory', 'renamed_directory')

Changing Current Working Directory (CWD)


 os.chdir(path): Changes the current working directory to the specified path. After
changing, all relative paths are resolved concerning the new working directory. If the
specified path does not exist, an OSError is raised.
For example, If we need to change the CWD to my_folder in D:/, then the following code
snippet is used.
importos
print("Current directory :", os.getcwd())
# Changing directory
os.chdir('/home/nikhil/Desktop/')
print("Current directory :", os.getcwd())
Output:
Current directory : /home/nikhil/Desktop/gfg
Current directory : /home/nikhil/Desktop
Listing Files in a Directory
 os.listdir(path): Returns a list of the names of files and directories in the specified
directory. The “.” refers to the current directory. Use “..” to refer to the parent directory.
The method does not recursively list contents of subdirectories. For recursion, use
os.walk().
For example: Listing the files in the CWD- GeeksforGeeks (root directory)
importos
print("Files in CWD are :",os.listdir(os.getcwd()))

Output
Files in CWD are : ['output.txt', 'input.txt', 'driver', 'Solution.py']
Removing a directory
 os.rmdir(path): Removes an empty directory. Use os.rmdir() for empty directories;
otherwise, an error is raised.
 shutil.rmtree(path): Removes a directory and its contents recursively. Use shutil.rmtree()
for non-empty directories. Be cautious as this is irreversible.
For example, Let us consider a directory K:/files. Now to remove it, one has to ensure
whether it is empty and then proceed for deleting.
importos
li=os.listdir('/')
if len(li)==0:
print("Error!! Directory not empty!!")
else:
os.rmdir('k:/files')
Check Whether It Is a Directory
 os.path.isdir(path): Returns True if the path is a directory, otherwise False. Often used
before performing operations like os.rmdir() or os.listdir() to ensure the path is valid.
Helps prevent errors during directory management.
importos
# current working directory of
cwd='/'
print(os.path.isdir(cwd))
# Some other directory
other='K:/'
print(os.path.isdir(other))

Output
True
False

Get Size of the Directory


 os.path.getsize(path): Returns the size of a file in bytes. Use this with os.walk() to
calculate directory size. os.walk() iterates through all subdirectories and files in a
directory.Summing up file sizes gives the total directory size.
importos
print(os.path.getsize(os.getcwd()))
Output4096
Getting Access and Modification Times
 os.path.getatime(path): Returns the last access time of a file or directory as a timestamp.
 os.path.getmtime(path): Returns the last modification time as a timestamp.
Example : Getting access and modification time of GeeksforGeeks (root) directory
importtime
importos
# Get times
access_time = os.path.getatime("/")
modification_time = os.path.getmtime("/")
# Convert to readable format
print("Access Time:", time.ctime(access_time))
print("Modification Time:", time.ctime(modification_time))

Output
Access Time: Sat Jan 4 09:21:37 2025
Modification Time: Sat Jan 4 09:21:37 2025
shutil module
shutil module in Python is a high-level file and directory management library. It provides
functions for copying, moving and removing files and directories.
shutil.copytree:
Recursively copies an entire directory tree (source directory and all its contents) to
a destination. Creates a new directory at the destination path and copies all files and
subdirectories. Raises FileExistsError if the destination exists and dirs_exist_ok is False.
Syntax:
shutil.copytree(src, dst, dirs_exist_ok=False)
Parameters:
 src: Path to the source directory.
 dst: Path to the destination directory.
 dirs_exist_ok: If True, allows copying into an existing directory. If False (default), raises
an error if the destination already exists.
Example:
importshutil
# Copy the entire directory tree
shutil.copytree("source_dir", "destination_dir")
print("Directory copied successfully")
shutil.rmtree:
Deletes an entire directory tree, including all its files and subdirectories. This operation is
irreversible. Be careful when specifying the path.
Syntax:
shutil.rmtree(path, ignore_errors=False)
Parameters:
 path: Path to the directory to be removed.
 ignore_errors: If True, ignores errors during removal. If False (default), raises an error.
Example:
importshutil
# Remove a directory tree
shutil.rmtree("destination_dir")
print("Directory removed successfully")
shutil.move(s,d):
Moves a file or directory to a new location. If the source and destination are on the same
filesystem, this is equivalent to renaming. Otherwise, it copies the source to the destination
and then deletes the original.
Syntax:
shutil.move(src, dst)
Parameters:
 src: Path to the source file or directory.
 dst: Path to the destination file or directory.
Example:
importshutil
# Move a directory to a new location
shutil.move("source_dir", "new_location")
print("Directory moved successfully")
Reading and Writing to text files in Python
Python provides built-in functions for creating, writing, and reading files. Two types of
files can be handled in Python, normal text files and binary files (written in binary
language, 0s, and 1s).
Text files: In this type of file, Each line of text is terminated with a special character
called EOL (End of Line), which is the new line character (‘\n’) in Python by default.
Binary files: In this type of file, there is no terminator for a line, and the data is stored
after converting it into machine-understandable binary language.
This article will focus on opening, closing, reading, and writing data in a text file. Here,
we will also see how to get Python output in a text file.
Table of Content
 Opening a Text File
 Read Text File
 Write to Text File
 Appending to a File
 Closing a Text File
 Opening a Text File in Python
It is done using the open() function. No module is required to be imported for this
function.
File_object = open(r"File_Name","Access_Mode")
Example: Here, file1 is created as an object for MyFile1 and file2 as object for MyFile2.
# Open function to open the file "MyFile1.txt"
# (same directory) in append mode and
file1 = open("MyFile1.txt","a")
# store its reference in the variable file1
# and "MyFile2.txt" in D:\Text in file2
file2 = open(r"D:\Text\MyFile2.txt","w+")
Reading From a File Using read()
read(): Returns the read bytes in form of a string. Reads n bytes, if no n specified, reads
the entire file.
File_object.read([n])
Reading a Text File Using readline()
readline(): Reads a line of the file and returns in form of a string.For specified n, reads at
most n bytes. However, does not reads more than one line, even if n exceeds the length of
the line.
File_object.readline([n])
Reading a File Using readlines()
readlines(): Reads all the lines and return them as each line a string element in a list.
File_object.readlines()
Note: ‘\n’ is treated as a special character of two bytes.
In this example, a file named “myfile.txt” is created and opened in write mode ( "w" ).
Data is written to the file using write and writelines methods. The file is then reopened in
read and append mode ( "r+" ). Various read operations,
including read , readline , readlines , and the use of seek , demonstrate different ways to
retrieve data from the file. Finally, the file is closed.
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
# \n is placed to indicate EOL (End of Line)
file1.write("Hello \n")
file1.writelines(L)
file1.close() # to change file access modes
file1 = open("myfile.txt", "r+")
print("Output of Read function is ")
print(file1.read())
print()
# seek(n) takes the file handle to the nth
# byte from the beginning.
file1.seek(0)
print("Output of Readline function is ")
print(file1.readline())
print()
file1.seek(0)
# To show difference between read and readline
print("Output of Read(9) function is ")
print(file1.read(9))
print()
file1.seek(0)
print("Output of Readline(9) function is ")
print(file1.readline(9))
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())
print()
file1.close()
Output:
Output of Read function is
Hello
This is Delhi
This is Paris
This is London
Output of Readline function is
Hello
Output of Read(9) function is
Hello
Output of Readline(9) function is
Hello
Output of Readlines function is
['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
Write to Text File in Python
There are two ways to write in a file:
Using write()
Using writelines()
Reference: write() VS writelines()
Writing to a Python Text File Using write()
write(): Inserts the string str1 in a single line in the text file.
File_object.write(str1)
file = open("Employees.txt", "w")
for i in range(3):
name = input("Enter the name of the employee: ")
file.write(name)
file.write("\n")
file.close()
print("Data is written into the file.")
Output:
Data is written into the file.
Writing to a Text File Using writelines()
writelines(): For a list of string elements, each string is inserted in the text file.Used to
insert multiple strings at a single time.
File_object.writelines(L) for L = [str1, str2, str3]
file1 = open("Employees.txt", "w")
lst = []
for i in range(3):
name = input("Enter the name of the employee: ")
lst.append(name + '\n')
file1.writelines(lst)
file1.close()
print("Data is written into the file.")
Output:
Data is written into the file.
Appending to a File in Python
In this example, a file named “myfile.txt” is initially opened in write mode ( "w" ) to write
lines of text. The file is then reopened in append mode ( "a" ), and “Today” is added to the
existing content. The output after appending is displayed using readlines . Subsequently,
the file is reopened in write mode, overwriting the content with “Tomorrow”. The final
output after writing is displayed using readlines.
file1 = open("myfile.txt", "w")
L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]
file1.writelines(L)
file1.close()
# Append-adds at last
file1 = open("myfile.txt", "a") # append mode
file1.write("Today \n")
file1.close()
file1 = open("myfile.txt", "r")
print("Output of Readlines after appending")
print(file1.readlines())
print()
file1.close()
# Write-Overwrites
file1 = open("myfile.txt", "w") # write mode
file1.write("Tomorrow \n")
file1.close()
file1 = open("myfile.txt", "r")
print("Output of Readlines after writing")
print(file1.readlines())
print()
file1.close()
Output:
Output of Readlines after appending
['This is Delhi \n', 'This is Paris \n', 'This is London \n', 'Today \n']
Output of Readlines after writing
['Tomorrow \n']
Reading and Writing CSV Files in Python
CSV (Comma Separated Values) format is the most common import and export
format for spreadsheets and databases. It is one of the most common methods for
exchanging data between applications and popular data format used in Data Science. It is
supported by a wide range of applications. A CSV file stores tabular data in which each
data field is separated by a delimiter(comma in most cases). To represent a CSV file, it
must be saved with the .csv file extension.
Reading from CSV file
Python contains a module called csv for the handling of CSV files. The reader class
from the module is used for reading data from a CSV file. At first, the CSV file is opened
using the open() method in ‘r’ mode(specifies read mode while opening a file) which
returns the file object then it is read by using the reader() method of CSV module that
returns the reader object that iterates throughout the lines in the specified CSV document.
Syntax:
csv.reader(csvfile, dialect='excel', **fmtparams
Note: The ‘with‘ keyword is used along with the open() method as it simplifies exception
handling and automatically closes the CSV file.
Example:
Consider the below CSV file –

import csv
# opening the CSV file
with open('Giants.csv', mode ='r')as file:
# reading the CSV file
csvFile = csv.reader(file)
# displaying the contents of the CSV file
for lines in csvFile:
print(lines)

Output:
[['Steve', 13, 'A'],
['John', 14, 'F'],
['Nancy', 14, 'C'],
['Ravi', 13, 'B']]
Writing to CSV file
csv.writer class is used to insert data to the CSV file. This class returns a writer
object which is responsible for converting the user’s data into a delimited string. A CSV
file object should be opened with newline=” otherwise, newline characters inside the
quoted fields will not be interpreted correctly.
Syntax:
csv.writer(csvfile, dialect='excel', **fmtparams)
csv.writer class provides two methods for writing to CSV. They
are writerow() and writerows().
writerow(): This method writes a single row at a time. Field row can be written using this
method.
Syntax:
writerow(fields)
writerows(): This method is used to write multiple rows at a time. This can be used to write
rows list.
Syntax:
writerows(rows)
Example:

# writing to CSV

import csv
fields = ['Name', 'Branch', 'Year', 'CGPA']

# data rows of csv file

rows = [ ['Nikhil', 'COE', '2', '9.0'],

['Sanchit', 'COE', '2', '9.1'],

['Aditya', 'IT', '2', '9.3'],

['Sagar', 'SE', '1', '9.5'],

['Prateek', 'MCE', '3', '7.8'],

['Sahil', 'EP', '2', '9.1']]

# name of csv file

filename = "university_records.csv"

# writing to csv file

with open(filename, 'w') as csvfile:

# creating a csv writer object

csvwriter = csv.writer(csvfile)

# writing the fields

csvwriter.writerow(fields)

# writing the data rows

csvwriter.writerows(rows)

Output:

We can also write dictionary to the CSV file. For this the CSV module provides the
csv.DictWriter class. This class returns a writer object which maps dictionaries onto output
rows.
Syntax:
csv.DictWriter(csvfile, fieldnames, restval=”, extrasaction=’raise’, dialect=’excel’, *args,
**kwds)
csv.DictWriter provides two methods for writing to CSV. They are:
writeheader(): writeheader() method simply writes the first row of your csv file using the
pre-specified fieldnames.
Syntax:
writeheader()
writerows(): writerows method simply writes all the rows but in each row, it writes only
the values(not keys).
Syntax:
writerows(mydict)
Example:

# importing the csv module


import csv
# my data rows as dictionary objects
mydict =[{'branch': 'COE', 'cgpa': '9.0', 'name': 'Nikhil', 'year': '2'},
{'branch': 'COE', 'cgpa': '9.1', 'name': 'Sanchit', 'year': '2'},
{'branch': 'IT', 'cgpa': '9.3', 'name': 'Aditya', 'year': '2'},
{'branch': 'SE', 'cgpa': '9.5', 'name': 'Sagar', 'year': '1'},
{'branch': 'MCE', 'cgpa': '7.8', 'name': 'Prateek', 'year': '3'},
{'branch': 'EP', 'cgpa': '9.1', 'name': 'Sahil', 'year': '2'}]
# field names
fields = ['name', 'branch', 'year', 'cgpa']
# name of csv file
filename = "university_records.csv"
# writing to csv file
with open(filename, 'w') as csvfile:
# creating a csv dict writer object
writer = csv.DictWriter(csvfile, fieldnames = fields)
# writing headers (field names)
writer.writeheader()
# writing data rows
writer.writerows(mydict)
Output:

What is JSON?
JSON is a text-based data format used to represent structured data based on key-
value pairs and arrays. It is widely used for data exchange between servers and web
clients, particularly in web applications and APIs. It is easy for both humans to read and
machines to parse and generate.
JSON Structure
A JSON file can have the following elements:
Objects: An object is an unordered set of key-value pairs, enclosed in curly braces {}.
Each key is a string, followed by a colon and its corresponding value. The key-value pairs
are separated by commas.
Example:
{
"name": "Alice",
"age": 30
}
Arrays: An array is an ordered list of values, enclosed in square brackets []. The values
can be of any data type and are separated by commas.
Example:
json
Copy
[
"apple",
"banana",
"cherry"
]
Values: A value can be a string, number, object, array, true, false, or null.
Example:
json
Copy
{
"active": true,
"address": null}
JSON Data Types
Here are the supported data types in JSON:
String: A sequence of characters enclosed in double quotes. Example: "Hello"
Number: Can be an integer or a floating-point number. Example: 25, 3.14
Object: A collection of key-value pairs enclosed in curly braces. Example: {"key":
"value"}
Array: An ordered list of values enclosed in square brackets. Example: ["apple",
"banana"]
Boolean: true or false
Null: Represents a null or empty value, written as null
JSON Example
Here's an example of a more complex JSON object that includes various types of data:
{
"name": "Alice",
"age": 30,
"email": "[email protected]",
"active": true,
"address": {
"street": "123 Main St",
"city": "Wonderland",
"zipcode": "12345"
},
"hobbies": ["reading", "hiking", "cooking"],
"friends": [
{
"name": "Bob",
"age": 25
},
{
"name": "Charlie",
"age": 28
}
]
}
Working with JSON in Python
Python provides the built-in json module for working with JSON data.
Loading JSON Data (Parsing JSON)
You can parse JSON data from a file or a string and convert it into Python objects (like
dictionaries or lists).
Example 1: Loading JSON from a File
import json
# Read the JSON data from a file
with open('data.json', 'r') as file:
data = json.load(file)
# Access the parsed data
print(data["name"]) # Output: Alice
Example 2: Loading JSON from a String
import json
# JSON string
json_string = '{"name": "Alice", "age": 30, "active": true}'
# Convert the JSON string into a Python dictionary
data = json.loads(json_string)
print(data["name"]) # Output: Alice
Writing JSON Data (Generating JSON)
You can convert Python objects (e.g., dictionaries, lists) to JSON format and write them to
a file or generate a JSON string.
Example 1: Writing JSON to a File
import json
# Python dictionary
data = {
"name": "Alice",
"age": 30,
"active": True
}
# Write the Python dictionary to a JSON file
with open('output.json', 'w') as file:
json.dump(data, file, indent=4) # indent=4 makes it pretty-printed
Example 2: Converting Python to JSON String
import json
# Python dictionary
data = {
"name": "Alice",
"age": 30,
"active": True
}
# Convert the Python dictionary into a JSON string
json_string = json.dumps(data, indent=4)
print(json_string)
Output (Pretty-Printed JSON):
{
"name": "Alice",
"age": 30,
"active": true
}
JSON Module Methods
json.load(): Reads JSON data from a file and returns it as a Python object (e.g., dictionary
or list).
json.loads(): Parses a JSON string and returns it as a Python object.
json.dump(): Converts a Python object into JSON format and writes it to a file.
json.dumps(): Converts a Python object into a JSON string.
Handling Complex Data Structures
JSON can be nested, meaning an object can contain other objects or arrays, and
arrays can contain objects. Here is an example:
{
"company": {
"name": "TechCorp",
"employees": [
{
"name": "Alice",
"position": "Developer"
},
{
"name": "Bob",
"position": "Manager"
}
]
}
}
In Python, you can access nested data like this:
import json
json_data = '''
{
"company": {
"name": "TechCorp",
"employees": [
{"name": "Alice", "position": "Developer"},
{"name": "Bob", "position": "Manager"}
]
}
}
'''
# Parse JSON
data = json.loads(json_data)
# Access nested values
company_name = data["company"]["name"]
employee_name = data["company"]["employees"][0]["name"]
print(company_name) # Output: TechCorp
print(employee_name) # Output: Alice
JSON vs. Other Formats (CSV, XML, etc.)
CSV is better suited for tabular data (e.g., spreadsheets).
JSON is more flexible and can represent hierarchical data structures, making it ideal for
complex data (e.g., configurations, APIs).
XML is similar to JSON but more verbose and is often used in legacy systems. JSON is
usually easier to work with due to its simpler syntax.
JSON Advantages
Human-readable: JSON is easy for humans to read and understand.
Compact: It's usually more compact than XML, making it efficient for storage and
transmission.
Language-agnostic: JSON is language-independent and supported by almost all
programming languages, including Python, JavaScript, Ruby, etc.
JSON Use Cases
Web APIs: JSON is the most common format for data exchange in RESTful APIs.
Configurations: Many applications and systems use JSON for configuration files
(e.g., .json files for settings).
Data Storage: JSON is used in document databases like MongoDB.
If you need more detailed information about any specific aspect of JSON, feel free to ask!
Top of Form

Bottom of Form
2. JSON (JavaScript Object Notation)
JSON is a lightweight data-interchange format that is easy to read and write. It is
often used for APIs and web services.
Reading a JSON file:
In Python, the json module can be used to handle JSON data.
import json
# Open and load the JSON file
with open('data.json', 'r') as file:
data = json.load(file)
# Print the loaded JSON data
print(data)
Writing to a JSON file:
import json
# Data to be written to JSON
data = {"name": "Alice", "age": 30}
# Open and write to a JSON file
with open('output.json', 'w') as file:
json.dump(data, file, indent=4) # `indent` makes the output more readable
Summary:
CSV is typically used for structured tabular data, with values separated by commas.
JSON is used for structured data with key-value pairs, often used for APIs and
configurations.
Both formats are easy to read and write using built-in libraries in Python (csv for CSV
files and json for JSON files), but pandas is often used for CSV files when more advanced
data manipulation is required.
Data Manipulation using MySQL:
Python MySQL Connector is a Python driver that helps to integrate Python and MySQL.
This Python MySQL library allows the conversion between Python and MySQL data
types. MySQL Connector API is implemented using pure Python and does not require any
third-party library.
This Python MySQL tutorial will help to learn how to use MySQL with Python from
basics to advance, including all necessary functions and queries explained in detail with
the help of good Python MySQL examples. So, let’s get started.
Installation
To install the Python-mysql-connector module, one must have Python and PIP,
preinstalled on their system. If Python and pip are already installed type the below
command in the terminal.
pip3 install mysql-connector-python
Connecting to MySQL Server
We can connect to the MySQL server using the connect() method.
Python3

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password"
)
print(dataBase)
# Disconnecting from the server
dataBase.close()

Output:
<mysql.connector.connection_cext.CMySQLConnection object at 0x7f73f0191d00>
Note: For more information, refer to Connect MySQL database using MySQL-Connector
Python.
Creating Database
After connecting to the MySQL server let’s see how to create a MySQL database using
Python. For this, we will first create a cursor() object and will then pass the SQL command
as a string to the execute() method. The SQL command to create a database is –
CREATE DATABASE DATABASE_NAME
Example: Creating MySQL database with Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
# creating database
cursorObject.execute("CREATE DATABASE gfg")

Output:
Creating Tables
For creating tables we will follow the similar approach of writing the SQL
commands as strings and then passing it to the execute() method of the cursor object. SQL
command for creating a table is –
CREATE TABLE
(
column_name_1 column_Data_type,
column_name_2 column_Data_type,
:
:
column_name_n column_Data_type
);
Example: Creating MySQL table using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
# creating table
studentRecord = """CREATE TABLE STUDENT (
NAME VARCHAR(20) NOT NULL,
BRANCH VARCHAR(50),
ROLL INT NOT NULL,
SECTION VARCHAR(5),
AGE INT
)"""
# table created
cursorObject.execute(studentRecord)
# disconnecting from server
dataBase.close()

Output:

Insert Data into Tables


To insert data into the MySQL table Insert into query is used.
Syntax:
INSERT INTO table_name (column_names) VALUES (data)
Example 1: Inserting Single Row

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\
VALUES (%s, %s, %s, %s, %s)"
val = ("Ram", "CSE", "85", "B", "19")
cursorObject.execute(sql, val)
dataBase.commit()
# disconnecting from server
dataBase.close()

Output:

Example 2: Inserting Multiple Rows


To insert multiple values at once, executemany() method is used. This method
iterates through the sequence of parameters, passing the current parameter to the execute
method.

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\
VALUES (%s, %s, %s, %s, %s)"
val = [("Nikhil", "CSE", "98", "A", "18"),
("Nisha", "CSE", "99", "A", "18"),
("Rohan", "MAE", "43", "B", "20"),
("Amit", "ECE", "24", "A", "21"),
("Anil", "MAE", "45", "B", "20"),
("Megha", "ECE", "55", "A", "22"),
("Sita", "CSE", "95", "A", "19")]
cursorObject.executemany(sql, val)
dataBase.commit()
# disconnecting from server
dataBase.close()

Output:

Fetching Data
We can use the select query on the MySQL tables in the following ways –
In order to select particular attribute columns from a table, we write the attribute names.
SELECT attr1, attr2 FROM table_name
In order to select all the attribute columns from a table, we use the asterisk ‘*’ symbol.
SELECT * FROM table_name
Example: Select data from MySQL table using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "SELECT NAME, ROLL FROM STUDENT"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
# disconnecting from server
dataBase.close()

Output:

Where Clause
Where clause is used in MySQL database to filter the data as per the condition required.
You can fetch, delete or update a particular set of data in MySQL database by using where
clause.
Syntax:
SELECT column1, column2, …. columnN FROM [TABLE NAME] WHERE
[CONDITION];
Example: Where clause in MySQL using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT where AGE >=20"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
# disconnecting from server
dataBase.close()

Output:
('Rohan', 'MAE', 43, 'B', 20)
('Amit', 'ECE', 24, 'A', 21)
('Anil', 'MAE', 45, 'B', 20)
('Megha', 'ECE', 55, 'A', 22)
Order By Clause:
OrderBy is used to arrange the result set in either ascending or descending order.
By default, it is always in ascending order unless “DESC” is mentioned, which arranges it
in descending order. “ASC” can also be used to explicitly arrange it in ascending order.
But, it is generally not done this way since default already does that.
Syntax:
SELECT column1, column2
FROM table_name
ORDER BY column_name ASC|DESC;
Example: Order By clause in MySQL using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT ORDER BY NAME DESC"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
# disconnecting from server
dataBase.close()

Output:

Limit Clause:
The Limit clause is used in SQL to control or limit the number of records in the
result set returned from the query generated. By default, SQL gives out the required
number of records starting from the top but it allows the use of OFFSET keyword.
OFFSET allows you to start from a custom row and get the required number of result
rows.
Syntax:
SELECT * FROM tablename LIMIT limit;
SELECT * FROM tablename LIMIT limit OFFSET offset;
Example: Limit Clause in MySQL using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "SELECT * FROM STUDENT LIMIT 2 OFFSET 1"
cursorObject.execute(query)
myresult = cursorObject.fetchall()
for x in myresult:
print(x)
# disconnecting from server
dataBase.close()

Output:
('Nikhil', 'CSE', 98, 'A', 18)
('Nisha', 'CSE', 99, 'A', 18)
Update Data
The update query is used to change the existing values in a database. By using update a
specific value can be corrected or updated. It only affects the data and not the structure of
the table. The basic advantage provided by this command is that it keeps the table
accurate.
Syntax:
UPDATE tablename
SET ="new value"
WHERE ="old value";
Example: Update MySQL table using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "UPDATE STUDENT SET AGE = 23 WHERE Name ='Ram'"
cursorObject.execute(query)
dataBase.commit()
# disconnecting from server
dataBase.close()

Output:

Delete Data from Table


We can use the Delete query to delete data from the table in MySQL.
Syntax:
DELETE FROM TABLE_NAME WHERE ATTRIBUTE_NAME =
ATTRIBUTE_VALUE
Example: Delete Data from MySQL table using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query = "DELETE FROM STUDENT WHERE NAME = 'Ram'"
cursorObject.execute(query)
dataBase.commit()
# disconnecting from server
dataBase.close()

Output:

Drop Tables
Drop command affects the structure of the table and not data. It is used to delete an
already existing table. For cases where you are not sure if the table to be dropped exists or
not DROP TABLE IF EXISTS command is used. Both cases will be dealt with in the
following examples.
Syntax:
DROP TABLE tablename;
DROP TABLE IF EXISTS tablename;
At first, let’s see the list of tables in our database.

We can see that there are two tables for students, so let’s drop the second table.
Example 1: Drop Table in MySQL using Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query ="DROP TABLE Student;"
cursorObject.execute(query)
dataBase.commit()
# disconnecting from server
dataBase.close()
Output:

Example 2: Drop Table if exists

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()
query ="Drop Table if exists Employee;"
cursorObject.execute(query)
dataBase.commit()
# disconnecting from server
dataBase.close()

The above example, will not create any error and output because we have used the
Drop Table is exists query. If we will simply use the Drop table Employee
then ProgrammingError: 1051 (42S02): Unknown table ‘gfg.Employee’ is raised.

You might also like