Understanding CSV
and Json files
PYTHO
N
Presented by : Harsh
Roll no. 2823915
Table Of Contents
Sr.No. Topic covered Slide no.
1 What are files , types 3
2 CSV Files and example 4 , 5
3 Reading from CSV files 6
4 Writing in CSV files 7
5 Jason files and examples 8,9
6 Reading from Json files 10
7 Writing in Json files 11
8 Conclusion 12
What Are Files?
• A file is a fundamental unit of storage in a computer
system that contains information or data. Files can store
various types of information, from plain text to complex
binary data.
Types:
•Text Files: store information as plain text
characters
Readable by humans, e.g., .txt, .csv.
•Binary Files: . They contain data in binary (0s and
1s)
Not directly readable by humans, e.g., images,
Introduction to CSV Files
What is CSV?
• CSV stands for Comma-Separated Values. It is a
widely used file format that facilitates the storage and
exchange of tabular data in a simple and human-
readable format.
• Structure:
• Each line represents a single data record (or row).
• Each record contains multiple fields (or columns),
separated by commas.
Example of CSV files
• Sample CSV Content (data.csv):
Name, Age, City
Alice, 30, New York
Bob, 25, Los Angeles
Charlie, 35, Chicago
Explanation:
• First row: Header (field names).
• Subsequent rows: Data records for each
individual.
Reading from CSV file
• Using the csv Module:
• Built-in module for handling CSV files.
import csv
Explanation:
# Read CSV file
• Csv.reader(file) :
Creates a reader object
With open ( 'data.csv', mode = 'r' ) as to iterate over lines in the
file : CSV file.
csv_reader = csv.reader (file) • print(row) : Outputs
each row as a list.
for row in csv_reader :
print (row)
Writing in csv files
Import csv
# Data to write Explanation:
data = [
[ ' Name ' , ' Age ' • Csv.writer(file): Creates
, ' City ' ] , a writer object to write data
[ ' Alice' , 30 , ' New York into the CSV file.
'],
[ ' Bob ' , 25 , ' Los • csv_writer.writerows(dat
Angeles ' ] , a: Writes multiple rows of
['Charlie', 35, 'Chicago'] data.
]
# Write to CSV file
With open ( ' output.csv ' , mode = '
newline = ' ' ) as file: csv _ writer = csv . Writer( file )
Csv _ writer. writerows( data )
What are Json files ?
• Definition:
• JSON (JavaScript Object Notation) file is a text file that stores
data in a structured format, which is easily readable and
writable by humans and machines.
• Structure:
• Uses key-value pairs.
• JSON files typically have a .json extension.
• contain data in key-value pairs, arrays,
and nested structures, allowing for complex
data representation.
Example of Json files
json
"people": • Key Points:
[ • Data is
organized
{"name": "Alice", "age": 30, "city": "New
hierarchically.
York"},
{"name": "Bob", "age": 25, "city": "Los • Supports nested
Angeles"} structures.
}
Reading from Json files
import json
# Open and read the JSON file
Explanation:
with open ( ' data.json ' , mode = ' r ' ) as file :
• The json module is
used to read JSON files.
data = json . Load ( file )
• load method parses the
# Accessing data
JSON file into a Python
dictionary
for person in data [ ' people ' ] :
Print ( person [ ' name ' ] , person [ ' age ' ] , person
[ ' city ' ] )
Writing to a Json file
import json
data = {
Explanation
"people" : The dump method
writes a Python
[ {"name": "Alice", "age": 30, "city": "New dictionary to a JSON
York"}, file with pretty
formatting.
{"name": "Bob", "age": 25, "city": "Los
Angeles"}
with open ( 'output.json', 'w') as file:
CONCLUSION
• Summary:
• Both CSV and JSON are essential formats for data storage
and exchange.
• CSV is best suited for flat, tabular data.
• JSON excels in representing complex, hierarchical data.
• Key Takeaways:
• Choose CSV for simple data and spreadsheet compatibility.
• Opt for JSON when dealing with structured or nested data.
• Understanding both formats enhances data handling
capabilities.