1.
CSV FILE HANDLING
Module Basics & Opening DictReader (Structured Reading)
- Import: import csv - [Link](file_obj)
- File Mode: Always use 'r' (read) or 'w' (write). - Maps info to a dictionary where keys = header row.
- Windows Line Ending Fix: You MUST use newline='' in open(). - row['name'] is safer than row[0].
with open('[Link]', 'w', newline='') as f: - Missing Headers: Pass fieldnames=['a','b'] param.
- Context Managers: 'with' ensures file closes automatically. - Example:
- Dialects: Defines separators (comma, tab, pipe). Can register new with open('[Link]') as f:
dialects for custom formats. reader = [Link](f)
for row in reader:
print(row['age'])
Reading Data (reader) DictWriter (Structured Writing)
- [Link](file_obj, delimiter=',') - [Link](f, fieldnames=['name', 'age'])
- Returns: Iterator of lists. Each list is a row. - header writing: [Link]() (Crucial Step)
- Handling Headers: - [Link]({'name':'A', 'age':10})
reader = [Link](f) - Handling Extras: extrasaction='ignore' or 'raise'.
header = next(reader) # Skips first row - Good for: Reordering columns easily.
for row in reader:
print(row[0], row[1])
- Error Handling: try-except [Link].
Advanced / Sniffer
- [Link](): Deduce format of unknown CSV.
Writing Data (writer) - snippet = [Link](1024)
- dialect = [Link]().sniff(snippet)
- [Link](file_obj, delimiter=',', quotechar='"') - [Link](0) # Reset pointer before reading!
- [Link](['a', 'b']): Writes one list as a row. - Skip Spaces: skipinitialspace=True ignores whitespace after
- [Link](list_of_lists): Bulk write. delimiter.
- Quoting: Controls when to add quotes.
csv.QUOTE_MINIMAL (Default)
csv.QUOTE_ALL (Quote everything)
csv.QUOTE_NONNUMERIC (Quote text only)
2. BINARY FILE HANDLING
Binary File Basics The Struct Module
- What: Images (png), Audio (mp3), Executables, Serialized data. - Purpose: Pack/Unpack binary data (C-struct style).
- Modes: 'rb', 'wb', 'ab', 'r+b'. - import struct
- Data Type: bytes, not str. - pack(fmt, v1, v2): Returns bytes.
- No Encoding: No utf-8/ascii decoding happens. - unpack(fmt, buffer): Returns tuple.
- Usage: [Link](n) reads n bytes. - calcsize(fmt): Returns size in bytes.
- Byte Literals: b'Hello' (ASCII only) or hex x00. - Format Codes:
'i' (int, 4b), 'f' (float, 4b), 'd' (double, 8b)
's' (char[]), '?' (bool).
'<' (Little Endian), '>' (Big Endian).
Pickle (Serialization)
- import pickle Bytes vs Bytearray
- Serialize: Object -> Bytes (dump)
- Deserialize: Bytes -> Object (load) - bytes(): Immutable.
- Methods: b = bytes([65, 66]) -> b'AB'
[Link](obj, file_obj) - bytearray(): Mutable.
obj = [Link](file_obj) ba = bytearray(b'AB')
[Link](obj) -> returns bytes string ba[0] = 67 -> b'CB'
- Protocol: pickle.HIGHEST_PROTOCOL for speed. - Methods: similar to string (find, split) but for bytes.
Pickle Security Random Access (Seek/Tell)
- WARNING: Never unpickle data from untrusted sources. - [Link](): Current byte position (int).
- Malicious pickle data can execute arbitrary code during loading. - [Link](offset, whence):
- Use JSON for untrusted data transfer instead. 0: Start (absolute)
1: Current (relative)
2: End (relative)
- [Link](size): Resize file to size bytes.
- MemoryView: memoryview(obj) allows zero-copy access to buffers.
3. NORMAL FILE HANDLING
Opening & Modes Writing Methods
- open(file, mode, encoding='utf-8') - write(string): Writes string. Returns char count.
- 'r': Read (Default). Fails if missing. - writelines(list): Writes iterable of strings.
- 'w': Write. Truncates (deletes content) or creates. *Warning*: Does NOT add newlines automatically.
- 'a': Append. Writes to end. Creates if missing. Correct: [Link](line + '\n' for line in lines)
- 'x': Exclusive Creation. Fails if exists. - Buffering: Files are buffered. Text doesn't appear on disk
- 'r+': Read & Write (No truncate). immediately.
- 'w+': Read & Write (Truncates first).
- 't': Text mode (auto decoding).
Flushing & Closing
Reading Methods - [Link](): Force write buffer to disk (useful for logs).
- [Link](): Essential to free resources.
- read(n): Read n chars (or all if empty). - Context Manager ('with'):
- readline(): Read until \n. Returns '' at EOF. Calls __enter__ and __exit__.
- readlines(): Returns list of all lines (kept in memory). Closes file even if exception occurs inside block.
- Iterating file object (Best Practice): Always prefer 'with' over manual close().
for line in f:
# Memory efficient line-by-line
Encodings
The OS Module - Default depends on OS (cp1252 on Win, utf-8 on Linux).
- ALWAYS specify encoding='utf-8' for portability.
- import os - Errors: open(..., errors='ignore'/'replace') handles decode errors.
- [Link]('[Link]'): Delete.
- [Link]('old', 'new'): Rename.
- [Link]('path'): Check existence.
- [Link]('path'): Check if file.
- [Link]('dir', 'file'): Path building.
- [Link](): Get current directory.
4. DICTIONARIES
Basics & Creation Merging & Updating
- Unordered (Py3.6-), Ordered (Py3.7+). - [Link](d2): Adds d2 to d1.
- Mutable, Indexed by Key. - Py 3.9+ Operators:
- Keys: Must be Hashable (int, str, tuple). merged = d1 | d2 (New dict)
- Creation: d1 |= d2 (In-place update)
d = {'a': 1} - Unpacking: {**d1, **d2} (Older merge method).
d = dict(a=1, b=2)
d = dict([('a', 1), ('b', 2)])
d = {x: x**2 for x in range(5)}
Advanced: Collections
- from collections import ...
Core Operations - defaultdict(type): Auto-creates missing keys.
dd = defaultdict(int)
- Access: d['k'] (Error if missing). dd['new'] += 1 (No KeyError, starts at 0)
- Safe Access: [Link]('k', default) (No Error). - Counter(iterable): Counts elements.
- Add/Mod: d['k'] = val. c = Counter('banana') -> {'a':3, 'n':2, 'b':1}
- Delete: del d['k'] or val = [Link]('k'). c.most_common(2)
- Pop Last: [Link]() (LIFO).
- Check: 'k' in d (Fast O(1) lookup).
Comprehensions & Sorting
Iteration & Views - {k: v for k,v in [Link]() if v > 0}
- Sorting:
- [Link](): View of keys. sorted(d) -> List of sorted keys.
- [Link](): View of values. dict(sorted([Link](), key=lambda item: item[1])) -> Sort by value.
- [Link](): View of (key, value) tuples.
- Loop:
for k, v in [Link](): ...
- Views are dynamic; they update if dict changes.
5. TUPLES
Definition & Immutability Named Tuples (Advanced)
- Ordered, Immutable sequence. - [Link]
- Syntax: t = (1, 2) or t = 1, 2 - Hybrid of tuple and object.
- Singleton: t = (1,) (Comma is mandatory). - Point = namedtuple('Point', ['x', 'y'])
- Immutability means hashable -> Can be dict keys. - p = Point(10, 20)
- Memory: Smaller overhead than lists. - Access: p.x and p.y (Readable) OR p[0].
- Speed: Creation/Iteration slightly faster than lists. - Attributes: p._asdict(), p._replace(x=5).
- Great for CSV rows or rigid data structures.
Operations
Tuples vs Lists
- Indexing: t[0], t[-1].
- Slicing: t[1:3]. - Use Tuple: Fixed data (Coords, Config, DB record), Dict keys,
- Concatenation: t1 + t2 (New tuple). Return multiple values from function.
- Repetition: t * 3. - Use List: Data that changes (Buffers, Accumulators).
- Membership: 5 in t. - Note: Tuple containing a list IS mutable inside.
- Methods: Only count(x) and index(x). t = (1, [2, 3])
t[1].append(4) -> Works!
Packing & Unpacking
Internals
- Pack: coordinates = 10, 20
- Unpack: x, y = coordinates - Stored in a single memory block.
- Swap: x, y = y, x - Fixed size means Python can optimize allocation.
- Star Unpacking: - type(t) is 'tuple'.
first, *rest = (1, 2, 3, 4)
first=1, rest=[2,3,4]
6. STRINGS
Definition & Properties Search & Replace
- Immutable sequence of Unicode characters. - find(sub): Returns index or -1.
- Quotes: '...', "...", """...""" (docstrings). - index(sub): Returns index or ValueError.
- Raw String: r'path\to\file' (Backslash is literal). - count(sub): Number of occurrences.
- Ord/Chr: ord('A')->65, chr(65)->'A'. - replace(old, new, [count]): Returns NEW string.
- Slicing: s[start:end:step]. s[::-1] reverses. - translate(table): High performance character mapping.
F-Strings (Formatting) Split & Join (Crucial)
- Syntax: f"Text {var} expr". - [Link](sep): Returns list. Default splits whitespace.
- Expressions: f"{x + 1}". - [Link](sep, maxsplit=1).
- Format Specifiers: - [Link](): Split from right.
f"{val:.2f}" (2 decimal float) - [Link](): Split by \n.
f"{x:<10}" (Left align, width 10) - [Link](sep): -> (before, sep, after).
f"{x:05d}" (Pad with zeros) - [Link](iterable): List -> String.
f"{date:%Y-%m-%d}" '-'.join(['a','b']) -> 'a-b'
- Debug: f"{var=}" prints "var=value".
Encoding & Decoding
Case & Check Methods
- Strings are Unicode. Files/Network are Bytes.
- lower(), upper(), capitalize(), title(), swapcase(). - Encode: str -> bytes
- isalpha() (Letters), isdigit() (Numbers). b = [Link]('utf-8')
- isalnum() (Letters+Nums), isspace(). - Decode: bytes -> str
- startswith(prefix), endswith(suffix). s = [Link]('utf-8')
- Errors: 'strict', 'ignore', 'replace'.
7. LISTS
Creation & Properties List Comprehensions
- Mutable, Ordered. - [expr for x in iter if cond]
- lst = [1, 'a', 3.14] - [x**2 for x in range(10) if x > 5]
- Constructor: list(iterable). - Nested: [x for row in grid for x in row]
- Slicing Assignment: - Speed: Faster than loop/append patterns.
nums[1:3] = [10, 20] (Replace slice) - Dict/Set Comps: {x:x} or {x}.
nums[:] = [] (Clear list)
- Copying:
b = a[:] (Shallow)
b = [Link]() (Shallow) Advanced Usage
import copy; [Link](a) (Deep)
- Stack: append() + pop().
- Queue: pop(0) is slow O(n). Use [Link].
- Filter/Map: list(map(func, lst)), list(filter(cond, lst)).
Adding & Removing - Any/All: any(lst) -> True if one True.
- Zip: zip(l1, l2) -> Iterator of tuples.
- append(x): O(1) at end.
- extend(iter): O(k) join list.
- insert(i, x): O(n) shift elements.
- pop(i): Remove at index (default last). Returns item. Common Pitfalls
- remove(x): Remove first x. Raises ValueError if not found.
- Modifying list while iterating:
- clear(): Remove all.
Don't: for i in lst: [Link](i)
Do: for i in lst[:]: ... (Iterate over copy)
- Mutable Default Args:
Ordering def f(l=[]): ... (Shared across calls)
def f(l=None): if l is None: l=[]
- sort(key=None, reverse=False): In-place stable sort.
- sorted(iterable): Returns NEW list.
- reverse(): In-place reverse.
- reversed(iterable): Returns iterator.