|
2 | 2 |
|
3 | 3 | ## Working with Text Files
|
4 | 4 |
|
| 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 | + |
5 | 47 | ## Open file using context manager
|
6 | 48 |
|
| 49 | +- Need not close the file explicitly |
| 50 | + |
| 51 | +```py |
| 52 | +with open('hello.txt') as f: |
| 53 | + # perform file operations |
| 54 | +``` |
| 55 | + |
7 | 56 | ## Working with CSV Files
|
8 | 57 |
|
| 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 | + |
9 | 101 | ## Working with JSON Files
|
10 | 102 |
|
| 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 | + |
11 | 138 | ## Other libraries and documentation
|
12 | 139 |
|
13 | 140 | - 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