Skip to content

Commit 72523a5

Browse files
committed
CSV
1 parent 15d6883 commit 72523a5

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

README.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,18 +1649,24 @@ b'.\n..\nfile1.txt\nfile2.txt\n'
16491649
CSV
16501650
---
16511651
```python
1652-
import csv
1653-
<reader> = csv.reader(<file>, dialect='excel', delimiter=',')
1654-
<list> = next(<reader>) # Returns next row as list of strings.
1652+
from csv import reader, writer
16551653
```
16561654

1655+
### Read
16571656
```python
1658-
<writer> = csv.writer(<file>, dialect='excel', delimiter=',')
1659-
<writer>.writerow(<collection>) # Encodes objects using `str(<el>)`.
1660-
<writer>.writerows(<coll_of_coll>)
1657+
<reader> = reader(<file>, dialect='excel', delimiter=',')
1658+
<list> = next(<reader>) # Returns next row as list of strings.
16611659
```
16621660
* **File must be opened with `'newline=""'` argument, or newlines embedded inside quoted fields will not be interpreted correctly!**
16631661

1662+
### Write
1663+
```python
1664+
<writer> = writer(<file>, dialect='excel', delimiter=',')
1665+
<writer>.writerow(<collection>) # Encodes objects using `str(<el>)`.
1666+
<writer>.writerows(<coll_of_coll>)
1667+
```
1668+
* **File must be opened with `'newline=""'` argument, or an extra '\r' will be added on platforms that use '\r\n' linendings!**
1669+
16641670
### Parameters
16651671
* **`'dialect'` - Master parameter that sets the default values.**
16661672
* **`'delimiter'` - A one-character string used to separate fields.**

index.html

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,18 +1479,24 @@
14791479
<span class="hljs-number">0</span>
14801480
</code></pre></div>
14811481

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-
&lt;reader&gt; = csv.reader(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>)
1484-
&lt;list&gt; = next(&lt;reader&gt;) <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">&lt;reader&gt; = reader(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>)
1486+
&lt;list&gt; = next(&lt;reader&gt;) <span class="hljs-comment"># Returns next row as list of strings.</span>
14851487
</code></pre></div>
14861488

1487-
<pre><code class="python language-python hljs">&lt;writer&gt; = csv.writer(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>)
1488-
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</span>
1489-
&lt;writer&gt;.writerows(&lt;coll_of_coll&gt;)
1490-
</code></pre>
14911489
<ul>
14921490
<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>
14931491
</ul>
1492+
<div><h3 id="write">Write</h3><pre><code class="python language-python hljs">&lt;writer&gt; = writer(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>)
1493+
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</span>
1494+
&lt;writer&gt;.writerows(&lt;coll_of_coll&gt;)
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>
14941500
<div><h3 id="parameters">Parameters</h3><ul>
14951501
<li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values.</strong></li>
14961502
<li><strong><code class="python hljs"><span class="hljs-string">'delimiter'</span></code> - A one-character string used to separate fields.</strong></li>
@@ -1566,7 +1572,7 @@
15661572
<ul>
15671573
<li><strong>New database will be created if path doesn't exist.</strong></li>
15681574
</ul>
1569-
<div><h3 id="read">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1575+
<div><h3 id="read-1">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
15701576
<span class="hljs-keyword">if</span> cursor:
15711577
&lt;tuple&gt; = cursor.fetchone() <span class="hljs-comment"># First row. Also next(cursor).</span>
15721578
&lt;list&gt; = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span>
@@ -1575,7 +1581,7 @@
15751581
<ul>
15761582
<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li>
15771583
</ul>
1578-
<div><h3 id="write">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
1584+
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
15791585
db.commit()
15801586
</code></pre></div>
15811587

0 commit comments

Comments
 (0)