Skip to content

Commit dc79bd9

Browse files
committed
Class, open, csv
1 parent 317d9a6 commit dc79bd9

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,7 @@ print(<el>)
935935
f'{<el>}'
936936
raise Exception(<el>)
937937
logging.debug(<el>)
938+
csv.writer(<file>).writerow([<el>])
938939
```
939940

940941
#### Repr() is used by:
@@ -1304,10 +1305,10 @@ Open
13041305
**Opens a file and returns a corresponding file object.**
13051306

13061307
```python
1307-
<file> = open('<path>', mode='r', encoding=None, endline=None)
1308+
<file> = open('<path>', mode='r', encoding=None, newline=None)
13081309
```
13091310
* **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.**
1310-
* **`'endline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.**
1311+
* **`'newline=None'` means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.**
13111312

13121313
### Modes
13131314
* **`'r'` - Read (default).**
@@ -1438,14 +1439,14 @@ import csv
14381439
### Read Rows from CSV File
14391440
```python
14401441
def read_csv_file(filename):
1441-
with open(filename, encoding='utf-8') as file:
1442+
with open(filename, encoding='utf-8', newline='') as file:
14421443
return csv.reader(file, delimiter=';')
14431444
```
14441445

14451446
### Write Rows to CSV File
14461447
```python
14471448
def write_to_csv_file(filename, rows):
1448-
with open(filename, 'w', encoding='utf-8') as file:
1449+
with open(filename, 'w', encoding='utf-8', newline='') as file:
14491450
writer = csv.writer(file, delimiter=';')
14501451
writer.writerows(rows)
14511452
```

index.html

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ <h4 id="strisusedby">Str() is used by:</h4>
888888
<span class="hljs-string">f'<span class="hljs-subst">{&lt;el&gt;}</span>'</span>
889889
<span class="hljs-keyword">raise</span> Exception(&lt;el&gt;)
890890
logging.debug(&lt;el&gt;)
891+
csv.writer(&lt;file&gt;).writerow([&lt;el&gt;])
891892
</code></pre>
892893
<h4 id="reprisusedby">Repr() is used by:</h4>
893894
<pre><code class="python language-python hljs">print([&lt;el&gt;])
@@ -1181,11 +1182,11 @@ <h3 id="argparse">Argparse</h3>
11811182
</ul>
11821183
<h2 id="open"><a href="#open" name="open">#</a>Open</h2>
11831184
<p><strong>Opens a file and returns a corresponding file object.</strong></p>
1184-
<pre><code class="python language-python hljs">&lt;file&gt; = open(<span class="hljs-string">'&lt;path&gt;'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, endline=<span class="hljs-keyword">None</span>)
1185+
<pre><code class="python language-python hljs">&lt;file&gt; = open(<span class="hljs-string">'&lt;path&gt;'</span>, mode=<span class="hljs-string">'r'</span>, encoding=<span class="hljs-keyword">None</span>, newline=<span class="hljs-keyword">None</span>)
11851186
</code></pre>
11861187
<ul>
11871188
<li><strong><code class="python hljs"><span class="hljs-string">'encoding=None'</span></code> means default encoding is used, which is platform dependent. Best practice is to use <code class="python hljs"><span class="hljs-string">'encoding="utf-8"'</span></code> whenever possible.</strong></li>
1188-
<li><strong><code class="python hljs"><span class="hljs-string">'endline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
1189+
<li><strong><code class="python hljs"><span class="hljs-string">'newline=None'</span></code> means all different end of line combinations are converted to '\n' on read, while on write all '\n' characters are converted to system's default line separator.</strong></li>
11891190
</ul>
11901191
<h3 id="modes">Modes</h3>
11911192
<ul>
@@ -1278,12 +1279,12 @@ <h2 id="csv"><a href="#csv" name="csv">#</a>CSV</h2>
12781279
</code></pre>
12791280
<h3 id="readrowsfromcsvfile">Read Rows from CSV File</h3>
12801281
<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>
1281-
<span class="hljs-keyword">with</span> open(filename, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1282+
<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:
12821283
<span class="hljs-keyword">return</span> csv.reader(file, delimiter=<span class="hljs-string">';'</span>)
12831284
</code></pre>
12841285
<h3 id="writerowstocsvfile">Write Rows to CSV File</h3>
12851286
<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>
1286-
<span class="hljs-keyword">with</span> open(filename, <span class="hljs-string">'w'</span>, encoding=<span class="hljs-string">'utf-8'</span>) <span class="hljs-keyword">as</span> file:
1287+
<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:
12871288
writer = csv.writer(file, delimiter=<span class="hljs-string">';'</span>)
12881289
writer.writerows(rows)
12891290
</code></pre>

0 commit comments

Comments
 (0)