|
1479 | 1479 | <span class="hljs-number">0</span>
|
1480 | 1480 | </code></pre></div>
|
1481 | 1481 |
|
1482 |
| -<div><h2 id="csv"><a href="#csv" name="csv">#</a>CSV</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> csv |
1483 |
| -<reader> = csv.reader(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
1484 |
| -<list> = next(<reader>) <span class="hljs-comment"># Returns next row as list of strings.</span> |
| 1482 | +<div><h2 id="csv"><a href="#csv" name="csv">#</a>CSV</h2><pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> csv <span class="hljs-keyword">import</span> reader, writer |
| 1483 | +</code></pre></div> |
| 1484 | + |
| 1485 | +<div><h3 id="read">Read</h3><pre><code class="python language-python hljs"><reader> = reader(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
| 1486 | +<list> = next(<reader>) <span class="hljs-comment"># Returns next row as list of strings.</span> |
1485 | 1487 | </code></pre></div>
|
1486 | 1488 |
|
1487 |
| -<pre><code class="python language-python hljs"><writer> = csv.writer(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
1488 |
| -<writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span> |
1489 |
| -<writer>.writerows(<coll_of_coll>) |
1490 |
| -</code></pre> |
1491 | 1489 | <ul>
|
1492 | 1490 | <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>
|
1493 | 1491 | </ul>
|
| 1492 | +<div><h3 id="write">Write</h3><pre><code class="python language-python hljs"><writer> = writer(<file>, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>) |
| 1493 | +<writer>.writerow(<collection>) <span class="hljs-comment"># Encodes objects using `str(<el>)`.</span> |
| 1494 | +<writer>.writerows(<coll_of_coll>) |
| 1495 | +</code></pre></div> |
| 1496 | + |
| 1497 | +<ul> |
| 1498 | +<li><strong>File must be opened with <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> argument, or an extra '\r' will be added on platforms that use '\r\n' linendings!</strong></li> |
| 1499 | +</ul> |
1494 | 1500 | <div><h3 id="parameters">Parameters</h3><ul>
|
1495 | 1501 | <li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values.</strong></li>
|
1496 | 1502 | <li><strong><code class="python hljs"><span class="hljs-string">'delimiter'</span></code> - A one-character string used to separate fields.</strong></li>
|
|
1566 | 1572 | <ul>
|
1567 | 1573 | <li><strong>New database will be created if path doesn't exist.</strong></li>
|
1568 | 1574 | </ul>
|
1569 |
| -<div><h3 id="read">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'<query>'</span>) |
| 1575 | +<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'<query>'</span>) |
1570 | 1576 | <span class="hljs-keyword">if</span> cursor:
|
1571 | 1577 | <tuple> = cursor.fetchone() <span class="hljs-comment"># First row. Also next(cursor).</span>
|
1572 | 1578 | <list> = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span>
|
|
1575 | 1581 | <ul>
|
1576 | 1582 | <li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li>
|
1577 | 1583 | </ul>
|
1578 |
| -<div><h3 id="write">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>) |
| 1584 | +<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'<query>'</span>) |
1579 | 1585 | db.commit()
|
1580 | 1586 | </code></pre></div>
|
1581 | 1587 |
|
|
0 commit comments