Skip to content

Commit c09cd27

Browse files
author
Sahil
committed
Add lecture 5 File/IO notes
1 parent 666f99c commit c09cd27

File tree

3 files changed

+140
-1
lines changed

3 files changed

+140
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ To discuss any doubts or questions after today's class with the community, join
1515
- Lecture 2: [[Notes]](/lecture2.md) [[Video]](https://www.youtube.com/watch?v=Q7nvvSlWem4) - Operators, Basic Control Flow - Conditionals, Loops and Functions
1616
- Lecture 3: [[Notes]](/lecture3.md) [[Video]](https://www.youtube.com/watch?v=tmxCgqNSj88) - Basic Data Structures and Containers: Lists, Tuples, Sets, Dictionaries
1717
- Lecture 4: [[Notes]](/lecture4.md) [[Video]](https://www.youtube.com/watch?v=NsOzuuLlR08) - Organizing Code & Dependency Management
18-
- Lecture 5: [[Notes]](/lecture5.md) [[Video]]() - File I/O, reading and writing text, csv and json files
18+
- Lecture 5: [[Notes]](/lecture5.md) [[Video]](https://www.youtube.com/watch?v=kj61srFJ9fY) - File I/O, reading and writing text, csv and json files

day5/todos.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"""
2+
The program fetches a json file and deserializes it
3+
"""
4+
5+
import json
6+
import requests
7+
8+
URL = "https://jsonplaceholder.typicode.com/todos"
9+
response = requests.get(URL)
10+
todos = json.loads(response.text)
11+
12+
print(todos[0])

lecture5.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,139 @@
22

33
## Working with Text Files
44

5+
- Used to store data such that it can be retrieved any time
6+
- To read or write from a file, the following operations take place:
7+
1. Open a file
8+
2. Perform Read or write operations
9+
3. Close the file
10+
11+
### Open a file
12+
13+
```py
14+
f = open('hello.txt', 'r')
15+
```
16+
17+
- First argument is the string containing file name
18+
- Second argument is the file opening mode. (`r`, `w`, `a`, etc.)
19+
- Default, files are opened in text mode.
20+
21+
### Reading from file
22+
23+
```py
24+
f.read(5) # read next 5 bytes
25+
f.readline() # read the entire line
26+
f.readlines() # read the entire file and return list of lines
27+
f.tell() # get the current file position
28+
f.seek(0) # bring file cursor to initial position
29+
```
30+
### Writing to file
31+
32+
```py
33+
f.write("hello world\n")
34+
```
35+
36+
### Closing a file
37+
38+
- Frees up the resources tied with the file
39+
40+
```py
41+
f.close()
42+
```
43+
44+
- Need to worry about exceptions if any operation on file is running
45+
- Use a `try...finally` block of code
46+
547
## Open file using context manager
648

49+
- Need not close the file explicitly
50+
51+
```py
52+
with open('hello.txt') as f:
53+
# perform file operations
54+
```
55+
756
## Working with CSV Files
857

58+
- CSV (Comma Separated Values) format is one of the most simplest ways to store tabular data.
59+
- Elements are separated by a delimiter, generally, a comma `,`.
60+
- The python's `csv` module makes it easier to operate on such files.
61+
62+
### Reading CSV File
63+
64+
```py
65+
import csv
66+
with open('marks.csv', 'r') as file:
67+
reader = csv.reader(file)
68+
for row in reader:
69+
print(row)
70+
```
71+
72+
- Explore `skipinitialspace` attribute in the reader function.
73+
- There can be many other attributes specified and can make code difficult to maintain, so to solve this `dialect` is provided as another optional parameter, which groups together many formatting patterns.
74+
75+
### Writing to CSV File
76+
77+
```py
78+
import csv
79+
with open('marks.csv', 'w') as file:
80+
writer = csv.writer(file)
81+
writer.writerow(["Roll No.", "Name", "Marks"])
82+
writer.writerow([1, "abc", 95])
83+
writer.writerow([2, "def", 97])
84+
```
85+
86+
- You can also try creating rowlist as list of lists and use the `writer.writerows` function.
87+
88+
### Read file as a dictionary
89+
90+
```py
91+
import csv
92+
with open('marks.csv', 'r') as file:
93+
csv_dict = csv.DictReader(file)
94+
for row in csv_dict:
95+
print(row)
96+
```
97+
98+
- Similarly, can try exploring `DictWriter`.
99+
- Also try to explore the `Pandas` library which is a popular library for data manipulation and analysis.
100+
9101
## Working with JSON Files
10102

103+
- Saving more complex data types like nested lists and dictionaries, parsing and serializing becomes complicated.
104+
- Python allows us to use the popular data interchange format JSON (JavaScript Object Notation) through a module `json`.
105+
- Converting python data to string is known as *serializing*.
106+
- Reconstructing the data from the string representation is called *deserializing*.
107+
108+
### Reading json from file
109+
110+
```py
111+
import json
112+
with open('marks.json') as file:
113+
data = json.load(file)
114+
print(data) # dict
115+
```
116+
117+
- `json.loads` can be used to convert string to dict.
118+
119+
### Writing json to file
120+
121+
```py
122+
import json
123+
124+
marks_dict = {
125+
"roll_no": 12,
126+
"name": "abc",
127+
"marks": 99
128+
}
129+
with open('marks.json', 'w') as json_file:
130+
json.dump(marks_dict, json_file)
131+
```
132+
133+
- `json.dumps` can be used to convert dict to string.
134+
- Can pass `indent` and `sort_keys` properties to pretty print the json data.
135+
136+
[Example code](day5/todos.py)
137+
11138
## Other libraries and documentation
12139

13140
- For manipulating path: [pathlib](https://docs.python.org/3/library/pathlib.html) and [os.path](https://docs.python.org/3/library/os.path.html)

0 commit comments

Comments
 (0)