Skip to content

Commit 01b975d

Browse files
committed
Csv
1 parent 3f5d246 commit 01b975d

File tree

2 files changed

+70
-7
lines changed

2 files changed

+70
-7
lines changed

README.md

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1640,21 +1640,54 @@ CSV
16401640
---
16411641
```python
16421642
import csv
1643+
<reader> = csv.reader(<file>, dialect='excel', delimiter=',', ...)
1644+
<list> = next(<reader>) # Returns a row as list of strings.
1645+
```
1646+
1647+
```python
1648+
<writer> = csv.writer(<file>, dialect='excel', delimiter=',', ...)
1649+
<writer>.writerow(<collection>)
1650+
<writer>.writerows(<coll_of_coll>)
1651+
```
1652+
1653+
### Parameters
1654+
* **`'dialect'` - Master parameter that sets the default values.**
1655+
* **`'delimiter'` - A one-character string used to separate fields.**
1656+
* **`'quotechar'` - Character for quoting fields that contain special characters.**
1657+
* **`'doublequote'` - Whether quotechars inside fields get doubled or escaped.**
1658+
* **`'skipinitialspace'` - Whether whitespace after delimiter gets stripped.**
1659+
* **`'lineterminator'` - How does writer terminate lines.**
1660+
* **`'quoting'` - Controls the amount of quoting: 0 - as necessary, 1 - all.**
1661+
* **`'escapechar'` - Character for escaping quotechar if doublequote is false.**
1662+
1663+
### Dialects
1664+
```python
1665+
+------------------+--------+-----------+--------------+
1666+
| | excel | excel_tab | unix_dialect |
1667+
+------------------+--------+-----------+--------------+
1668+
| delimiter | ',' | '\t' | ',' |
1669+
| quotechar | '"' | '"' | '"' |
1670+
| doublequote | True | True | True |
1671+
| skipinitialspace | False | False | False |
1672+
| lineterminator | '\r\n' | '\r\n' | '\n' |
1673+
| quoting | 0 | 0 | 1 |
1674+
| escapechar | None | None | None |
1675+
+------------------+--------+-----------+--------------+
16431676
```
16441677

16451678
### Read Rows from CSV File
16461679
```python
16471680
def read_csv_file(filename):
16481681
with open(filename, encoding='utf-8', newline='') as file:
1649-
return csv.reader(file, delimiter=';')
1682+
return csv.reader(file)
16501683
```
16511684
* **If `'newline=""'` is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.**
16521685

16531686
### Write Rows to CSV File
16541687
```python
16551688
def write_to_csv_file(filename, rows):
16561689
with open(filename, 'w', encoding='utf-8', newline='') as file:
1657-
writer = csv.writer(file, delimiter=';')
1690+
writer = csv.writer(file)
16581691
writer.writerows(rows)
16591692
```
16601693

@@ -2138,7 +2171,7 @@ Table
21382171
from tabulate import tabulate
21392172
import csv
21402173
with open(<filename>, encoding='utf-8', newline='') as file:
2141-
lines = csv.reader(file, delimiter=';')
2174+
lines = csv.reader(file)
21422175
headers = [header.title() for header in next(lines)]
21432176
table = tabulate(lines, headers)
21442177
print(table)

index.html

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,19 +1466,49 @@ <h4 id="usingsubprocess">Using subprocess:</h4>
14661466
</code></pre>
14671467
<h2 id="csv"><a href="#csv" name="csv">#</a>CSV</h2>
14681468
<pre><code class="python language-python hljs"><span class="hljs-keyword">import</span> csv
1469+
&lt;reader&gt; = csv.reader(&lt;file&gt;, dialect=<span class="hljs-string">'excel'</span>, delimiter=<span class="hljs-string">','</span>, ...)
1470+
&lt;list&gt; = next(&lt;reader&gt;) <span class="hljs-comment"># Returns a row as list of strings.</span>
1471+
</code></pre>
1472+
<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>, ...)
1473+
&lt;writer&gt;.writerow(&lt;collection&gt;)
1474+
&lt;writer&gt;.writerows(&lt;coll_of_coll&gt;)
1475+
</code></pre>
1476+
<h3 id="parameters">Parameters</h3>
1477+
<ul>
1478+
<li><strong><code class="python hljs"><span class="hljs-string">'dialect'</span></code> - Master parameter that sets the default values.</strong></li>
1479+
<li><strong><code class="python hljs"><span class="hljs-string">'delimiter'</span></code> - A one-character string used to separate fields.</strong></li>
1480+
<li><strong><code class="python hljs"><span class="hljs-string">'quotechar'</span></code> - Character for quoting fields that contain special characters.</strong></li>
1481+
<li><strong><code class="python hljs"><span class="hljs-string">'doublequote'</span></code> - Whether quotechars inside fields get doubled or escaped.</strong></li>
1482+
<li><strong><code class="python hljs"><span class="hljs-string">'skipinitialspace'</span></code> - Whether whitespace after delimiter gets stripped.</strong></li>
1483+
<li><strong><code class="python hljs"><span class="hljs-string">'lineterminator'</span></code> - How does writer terminate lines.</strong></li>
1484+
<li><strong><code class="python hljs"><span class="hljs-string">'quoting'</span></code> - Controls the amount of quoting: 0 - as necessary, 1 - all.</strong></li>
1485+
<li><strong><code class="python hljs"><span class="hljs-string">'escapechar'</span></code> - Character for escaping quotechar if doublequote is false.</strong></li>
1486+
</ul>
1487+
<h3 id="dialects">Dialects</h3>
1488+
<pre><code class="python language-python hljs">+------------------+--------+-----------+--------------+
1489+
| | excel | excel_tab | unix_dialect |
1490+
+------------------+--------+-----------+--------------+
1491+
| delimiter | <span class="hljs-string">','</span> | <span class="hljs-string">'\t'</span> | <span class="hljs-string">','</span> |
1492+
| quotechar | <span class="hljs-string">'"'</span> | <span class="hljs-string">'"'</span> | <span class="hljs-string">'"'</span> |
1493+
| doublequote | <span class="hljs-keyword">True</span> | <span class="hljs-keyword">True</span> | <span class="hljs-keyword">True</span> |
1494+
| skipinitialspace | <span class="hljs-keyword">False</span> | <span class="hljs-keyword">False</span> | <span class="hljs-keyword">False</span> |
1495+
| lineterminator | <span class="hljs-string">'\r\n'</span> | <span class="hljs-string">'\r\n'</span> | <span class="hljs-string">'\n'</span> |
1496+
| quoting | <span class="hljs-number">0</span> | <span class="hljs-number">0</span> | <span class="hljs-number">1</span> |
1497+
| escapechar | <span class="hljs-keyword">None</span> | <span class="hljs-keyword">None</span> | <span class="hljs-keyword">None</span> |
1498+
+------------------+--------+-----------+--------------+
14691499
</code></pre>
14701500
<h3 id="readrowsfromcsvfile">Read Rows from CSV File</h3>
14711501
<pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">read_csv_file</span><span class="hljs-params">(filename)</span>:</span>
14721502
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
1473-
<span class="hljs-keyword">return</span> csv.reader(file, delimiter=<span class="hljs-string">';'</span>)
1503+
<span class="hljs-keyword">return</span> csv.reader(file)
14741504
</code></pre>
14751505
<ul>
14761506
<li><strong>If <code class="python hljs"><span class="hljs-string">'newline=""'</span></code> is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.</strong></li>
14771507
</ul>
14781508
<h3 id="writerowstocsvfile">Write Rows to CSV File</h3>
14791509
<pre><code class="python language-python hljs"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">write_to_csv_file</span><span class="hljs-params">(filename, rows)</span>:</span>
14801510
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
1481-
writer = csv.writer(file, delimiter=<span class="hljs-string">';'</span>)
1511+
writer = csv.writer(file)
14821512
writer.writerows(rows)
14831513
</code></pre>
14841514
<h2 id="json"><a href="#json" name="json">#</a>JSON</h2>
@@ -1681,7 +1711,7 @@ <h3 id="attributes-1">Attributes</h3>
16811711
value = getattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>)
16821712
setattr(&lt;object&gt;, <span class="hljs-string">'&lt;attr_name&gt;'</span>, value)
16831713
</code></pre>
1684-
<h3 id="parameters">Parameters</h3>
1714+
<h3 id="parameters-1">Parameters</h3>
16851715
<pre><code class="python language-python hljs"><span class="hljs-keyword">from</span> inspect <span class="hljs-keyword">import</span> signature
16861716
&lt;sig&gt; = signature(&lt;function&gt;)
16871717
no_of_params = len(&lt;sig&gt;.parameters)
@@ -1835,7 +1865,7 @@ <h4 id="printsacsvfileasanasciitable">Prints a CSV file as an ASCII table:</h4>
18351865
<span class="hljs-keyword">from</span> tabulate <span class="hljs-keyword">import</span> tabulate
18361866
<span class="hljs-keyword">import</span> csv
18371867
<span class="hljs-keyword">with</span> open(&lt;filename&gt;, encoding=<span class="hljs-string">'utf-8'</span>, newline=<span class="hljs-string">''</span>) <span class="hljs-keyword">as</span> file:
1838-
lines = csv.reader(file, delimiter=<span class="hljs-string">';'</span>)
1868+
lines = csv.reader(file)
18391869
headers = [header.title() <span class="hljs-keyword">for</span> header <span class="hljs-keyword">in</span> next(lines)]
18401870
table = tabulate(lines, headers)
18411871
print(table)

0 commit comments

Comments
 (0)