From dc79bd9fdb3b6ed0a586c1f4eedeafa139dc96cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jure=20=C5=A0orn?= Date: Tue, 2 Jul 2019 10:35:05 +0200 Subject: [PATCH] Class, open, csv --- README.md | 9 +++++---- index.html | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 55dac19..02577f3 100644 --- a/README.md +++ b/README.md @@ -935,6 +935,7 @@ print() f'{}' raise Exception() logging.debug() +csv.writer().writerow([]) ``` #### Repr() is used by: @@ -1304,10 +1305,10 @@ Open **Opens a file and returns a corresponding file object.** ```python - = open('', mode='r', encoding=None, endline=None) + = open('', mode='r', encoding=None, newline=None) ``` * **`'encoding=None'` means default encoding is used, which is platform dependent. Best practice is to use `'encoding="utf-8"'` whenever possible.** -* **`'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.** +* **`'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.** ### Modes * **`'r'` - Read (default).** @@ -1438,14 +1439,14 @@ import csv ### Read Rows from CSV File ```python def read_csv_file(filename): - with open(filename, encoding='utf-8') as file: + with open(filename, encoding='utf-8', newline='') as file: return csv.reader(file, delimiter=';') ``` ### Write Rows to CSV File ```python def write_to_csv_file(filename, rows): - with open(filename, 'w', encoding='utf-8') as file: + with open(filename, 'w', encoding='utf-8', newline='') as file: writer = csv.writer(file, delimiter=';') writer.writerows(rows) ``` diff --git a/index.html b/index.html index bb6de35..999be7c 100644 --- a/index.html +++ b/index.html @@ -888,6 +888,7 @@ creature = Creature(Point(0, f'{<el>}' raise Exception(<el>) logging.debug(<el>) +csv.writer(<file>).writerow([<el>])

Repr() is used by:

print([<el>])
@@ -1181,11 +1182,11 @@ value = args.<name>
 
 

#Open

Opens a file and returns a corresponding file object.

-
<file> = open('<path>', mode='r', encoding=None, endline=None)
+
<file> = open('<path>', mode='r', encoding=None, newline=None)
 
  • 'encoding=None' means default encoding is used, which is platform dependent. Best practice is to use 'encoding="utf-8"' whenever possible.
  • -
  • '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.
  • +
  • '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.

Modes

    @@ -1278,12 +1279,12 @@ value = args.<name>

Read Rows from CSV File

def read_csv_file(filename):
-    with open(filename, encoding='utf-8') as file:
+    with open(filename, encoding='utf-8', newline='') as file:
         return csv.reader(file, delimiter=';')
 

Write Rows to CSV File

def write_to_csv_file(filename, rows):
-    with open(filename, 'w', encoding='utf-8') as file:
+    with open(filename, 'w', encoding='utf-8', newline='') as file:
         writer = csv.writer(file, delimiter=';')
         writer.writerows(rows)