|
1590 | 1590 | </code></pre></div>
|
1591 | 1591 |
|
1592 | 1592 |
|
1593 |
| -<div><h3 id="read">Read</h3><pre><code class="python language-python hljs"><reader> = csv.reader(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
| 1593 | +<div><h3 id="read">Read</h3><pre><code class="python language-python hljs"><reader> = csv.reader(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span> |
1594 | 1594 | <list> = next(<reader>) <span class="hljs-comment"># Returns next row as a list of strings.</span>
|
1595 | 1595 | <list> = list(<reader>) <span class="hljs-comment"># Returns list of remaining rows.</span>
|
1596 | 1596 | </code></pre></div>
|
1597 | 1597 |
|
1598 | 1598 | <ul>
|
1599 | 1599 | <li><strong>File must be opened with <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or newlines embedded inside quoted fields will not be interpreted correctly!</strong></li>
|
1600 | 1600 | </ul>
|
1601 |
| -<div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = csv.writer(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
| 1601 | +<div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = csv.writer(<file>) <span class="hljs-comment"># Also: `dialect='excel', delimiter=','`.</span> |
1602 | 1602 | <writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span>
|
1603 | 1603 | <writer>.writerows(<coll_of_coll>) <span class="hljs-comment"># Appends multiple rows.</span>
|
1604 | 1604 | </code></pre></div>
|
|
1684 | 1684 |
|
1685 | 1685 | <div><h3 id="mysql">MySQL</h3><p><strong>Has a very similar interface, with differences listed below.</strong></p><pre><code class="python language-python hljs"><span class="hljs-comment"># $ pip3 install mysql-connector</span>
|
1686 | 1686 | <span class="hljs-keyword">from</span> mysql <span class="hljs-keyword">import</span> connector
|
1687 |
| -db = connector.connect(host=<str>, user=<str>, password=<str>, database=<str>) |
1688 |
| -<cursor> = db.cursor() |
| 1687 | +db = connector.connect(host=<str>, …) <span class="hljs-comment"># `user=<str>, password=<str>, database=<str>`.</span> |
| 1688 | +<cursor> = db.cursor() <span class="hljs-comment"># Only cursor has execute method.</span> |
1689 | 1689 | <cursor>.execute(<span class="hljs-string">'<query>'</span>) <span class="hljs-comment"># Can raise a subclass of connector.Error.</span>
|
1690 | 1690 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <list/tuple>) <span class="hljs-comment"># Replaces '%s's in query with values.</span>
|
1691 | 1691 | <cursor>.execute(<span class="hljs-string">'<query>'</span>, <dict/namedtuple>) <span class="hljs-comment"># Replaces '%(<key>)s's with values.</span>
|
|
1701 | 1701 |
|
1702 | 1702 | <div><h3 id="encode-1">Encode</h3><pre><code class="python language-python hljs"><bytes> = bytes(<coll_of_ints>) <span class="hljs-comment"># Ints must be in range from 0 to 255.</span>
|
1703 | 1703 | <bytes> = bytes(<str>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <str>.encode('utf-8')</span>
|
1704 |
| -<bytes> = <int>.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`</span> |
| 1704 | +<bytes> = <int>.to_bytes(n_bytes, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> |
1705 | 1705 | <bytes> = bytes.fromhex(<span class="hljs-string">'<hex>'</span>) <span class="hljs-comment"># Hex numbers can be separated by spaces.</span>
|
1706 | 1706 | </code></pre></div>
|
1707 | 1707 |
|
1708 | 1708 | <div><h3 id="decode-1">Decode</h3><pre><code class="python language-python hljs"><list> = list(<bytes>) <span class="hljs-comment"># Returns ints in range from 0 to 255.</span>
|
1709 | 1709 | <str> = str(<bytes>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Or: <bytes>.decode('utf-8')</span>
|
1710 |
| -<int> = int.from_bytes(<bytes>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`</span> |
| 1710 | +<int> = int.from_bytes(<bytes>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> |
1711 | 1711 | <span class="hljs-string">'<hex>'</span> = <bytes>.hex() <span class="hljs-comment"># Returns a string of hexadecimal numbers.</span>
|
1712 | 1712 | </code></pre></div>
|
1713 | 1713 |
|
|
1786 | 1786 |
|
1787 | 1787 | <pre><code class="python language-python hljs"><list> = list(<mview>) <span class="hljs-comment"># Returns list of ints or floats.</span>
|
1788 | 1788 | <str> = str(<mview>, <span class="hljs-string">'utf-8'</span>) <span class="hljs-comment"># Treats mview as a bytes object.</span>
|
1789 |
| -<int> = int.from_bytes(<mview>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`</span> |
| 1789 | +<int> = int.from_bytes(<mview>, …) <span class="hljs-comment"># `byteorder='big/little', signed=False`.</span> |
1790 | 1790 | <span class="hljs-string">'<hex>'</span> = <mview>.hex() <span class="hljs-comment"># Treats mview as a bytes object.</span>
|
1791 | 1791 | </code></pre>
|
1792 | 1792 | <div><h2 id="deque"><a href="#deque" name="deque">#</a>Deque</h2><p><strong>A thread-safe list with efficient appends and pops from either side. Pronounced "deck".</strong></p><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> deque
|
|
0 commit comments