0% found this document useful (0 votes)
14 views

csv_json

The document provides an overview of various Python functions and modules related to file operations, including binary file handling with the pickle module and CSV file manipulation using the csv library. It explains the functionalities of functions like dump, load, and methods for reading CSV files. Additionally, it introduces JSON, detailing how to serialize and deserialize Python objects using json functions.

Uploaded by

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

csv_json

The document provides an overview of various Python functions and modules related to file operations, including binary file handling with the pickle module and CSV file manipulation using the csv library. It explains the functionalities of functions like dump, load, and methods for reading CSV files. Additionally, it introduces JSON, detailing how to serialize and deserialize Python objects using json functions.

Uploaded by

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

flush() function

strip() - removes the given character form both ends.


lstrip() - removes given character from left end.
rstrip() - removes given character from right end.

File Pointer

Binary file operations

pickle

byte stream -> pickle


read -> unpickle from byte stream to the original format

list1 = [1,2,3,"hello",3.5]
Python module pickle

This pickle module provides to two main functions

1)dump(object_to_write,filehandle)
2)load() - can be used read the dumped data from file

1. Inserting/appending a record in a binary file


2. Reading records from a binary file
3. Search a record in a binary file
4. Update a record in a binary file

CSV (Comma Separated Values)

Read CSV file using csv.reader


Read CSV file using readlines()
Read CSV file using Pandas
Read CSV file using csv.DictReader()

import csv

CSV - Comma Separated Values

Each line in a file is known as data/record.

Each record consists of one or more fields, separated by commas (known as


delimiters).

Tabular data is stored as text in a CSV file.

csv.reader

csv1 = csv.reader(file)

empty list
next() - Used to obtain the header
next() - returns the current row and moves to the next row.

r - to read an existing file


w - to create a new file
a - appending
------------

DictReader - Used to read the CSV files in dictionary format

A dictionary - Key value - Key : Value

----------------------------------
What is JSON?

JavaScript Object Notation.


JSON Objects Python Equivalent
Object Dictionary(dict)
Array List(list)
String String(str)
Number int,float
Boolean true True
Boolean false False
Null None

json.dumps() - This function is used to serialize(encode) a Python object into a


JSON formatted string.

json.dump() - This function is used to serialize a Python object and write it to a


JSON file.

json.loads() - This function is used to parse(decode) a JSON formatted string and


convert it
into a Python object.

json.load() - This function is used to read a JSON file and parse its contents
into a Python object

requests - It allows us to send HTTP requests using Python

You might also like