Browse Source

CSV

pull/41/head
Jure Šorn 5 years ago
parent
commit
72523a53fd
2 changed files with 27 additions and 15 deletions
  1. 18
      README.md
  2. 24
      index.html

18
README.md

@ -1649,18 +1649,24 @@ b'.\n..\nfile1.txt\nfile2.txt\n'
CSV
---
```python
import csv
<reader> = csv.reader(<file>, dialect='excel', delimiter=',')
<list> = next(<reader>) # Returns next row as list of strings.
from csv import reader, writer
```
### Read
```python
<writer> = csv.writer(<file>, dialect='excel', delimiter=',')
<writer>.writerow(<collection>) # Encodes objects using `str(<el>)`.
<writer>.writerows(<coll_of_coll>)
<reader> = reader(<file>, dialect='excel', delimiter=',')
<list> = next(<reader>) # Returns next row as list of strings.
```
* **File must be opened with `'newline=""'` argument, or newlines embedded inside quoted fields will not be interpreted correctly!**
### Write
```python
<writer> = writer(<file>, dialect='excel', delimiter=',')
<writer>.writerow(<collection>) # Encodes objects using `str(<el>)`.
<writer>.writerows(<coll_of_coll>)
```
* **File must be opened with `'newline=""'` argument, or an extra '\r' will be added on platforms that use '\r\n' linendings!**
### Parameters
* **`'dialect'` - Master parameter that sets the default values.**
* **`'delimiter'` - A one-character string used to separate fields.**

24
index.html

@ -1479,18 +1479,24 @@ os.replace(from, to) <span class="hljs-comment"># Same, but overwr
<span class="hljs-number">0</span>
</code></pre></div>
<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
&lt;reader&gt; = csv.reader(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>)
&lt;list&gt; = next(&lt;reader&gt;) <span class="hljs-comment"># Returns next row as list of strings.</span>
<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
</code></pre></div>
<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>)
&lt;list&gt; = next(&lt;reader&gt;) <span class="hljs-comment"># Returns next row as list of strings.</span>
</code></pre></div>
<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>)
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</span>
&lt;writer&gt;.writerows(&lt;coll_of_coll&gt;)
</code></pre>
<ul>
<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>
</ul>
<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>)
&lt;writer&gt;.writerow(&lt;collection&gt;) <span class="hljs-comment"># Encodes objects using `str(&lt;el&gt;)`.</span>
&lt;writer&gt;.writerows(&lt;coll_of_coll&gt;)
</code></pre></div>
<ul>
<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>
</ul>
<div><h3 id="parameters">Parameters</h3><ul>
<li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values.</strong></li>
<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 @@ db.close()
<ul>
<li><strong>New database will be created if path doesn't exist.</strong></li>
</ul>
<div><h3 id="read">Read</h3><pre><code class="python language-python hljs">cursor = db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
<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>)
<span class="hljs-keyword">if</span> cursor:
&lt;tuple&gt; = cursor.fetchone() <span class="hljs-comment"># First row. Also next(cursor).</span>
&lt;list&gt; = cursor.fetchall() <span class="hljs-comment"># Remaining rows.</span>
@ -1575,7 +1581,7 @@ db.close()
<ul>
<li><strong>Returned values can be of type str, int, float, bytes or None.</strong></li>
</ul>
<div><h3 id="write">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
<div><h3 id="write-1">Write</h3><pre><code class="python language-python hljs">db.execute(<span class="hljs-string">'&lt;query&gt;'</span>)
db.commit()
</code></pre></div>

Loading…
Cancel
Save