diff --git a/README.md b/README.md index 56ee8be..b74355a 100644 --- a/README.md +++ b/README.md @@ -1640,13 +1640,46 @@ CSV --- ```python import csv + = csv.reader(, dialect='excel', delimiter=',', ...) + = next() # Returns a row as list of strings. +``` + +```python + = csv.writer(, dialect='excel', delimiter=',', ...) +.writerow() +.writerows() +``` + +### Parameters +* **`'dialect'` - Master parameter that sets the default values.** +* **`'delimiter'` - A one-character string used to separate fields.** +* **`'quotechar'` - Character for quoting fields that contain special characters.** +* **`'doublequote'` - Whether quotechars inside fields get doubled or escaped.** +* **`'skipinitialspace'` - Whether whitespace after delimiter gets stripped.** +* **`'lineterminator'` - How does writer terminate lines.** +* **`'quoting'` - Controls the amount of quoting: 0 - as necessary, 1 - all.** +* **`'escapechar'` - Character for escaping quotechar if doublequote is false.** + +### Dialects +```python ++------------------+--------+-----------+--------------+ +| | excel | excel_tab | unix_dialect | ++------------------+--------+-----------+--------------+ +| delimiter | ',' | '\t' | ',' | +| quotechar | '"' | '"' | '"' | +| doublequote | True | True | True | +| skipinitialspace | False | False | False | +| lineterminator | '\r\n' | '\r\n' | '\n' | +| quoting | 0 | 0 | 1 | +| escapechar | None | None | None | ++------------------+--------+-----------+--------------+ ``` ### Read Rows from CSV File ```python def read_csv_file(filename): with open(filename, encoding='utf-8', newline='') as file: - return csv.reader(file, delimiter=';') + return csv.reader(file) ``` * **If `'newline=""'` is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.** @@ -1654,7 +1687,7 @@ def read_csv_file(filename): ```python def write_to_csv_file(filename, rows): with open(filename, 'w', encoding='utf-8', newline='') as file: - writer = csv.writer(file, delimiter=';') + writer = csv.writer(file) writer.writerows(rows) ``` @@ -2138,7 +2171,7 @@ Table from tabulate import tabulate import csv with open(, encoding='utf-8', newline='') as file: - lines = csv.reader(file, delimiter=';') + lines = csv.reader(file) headers = [header.title() for header in next(lines)] table = tabulate(lines, headers) print(table) diff --git a/index.html b/index.html index 3d46521..f28132d 100644 --- a/index.html +++ b/index.html @@ -1466,11 +1466,41 @@ os.replace(from, to) #CSV
import csv
+<reader> = csv.reader(<file>, dialect='excel', delimiter=',', ...)
+<list>   = next(<reader>)  # Returns a row as list of strings.
+
+
<writer> = csv.writer(<file>, dialect='excel', delimiter=',', ...)
+<writer>.writerow(<collection>)
+<writer>.writerows(<coll_of_coll>)
+
+

Parameters

+
    +
  • 'dialect' - Master parameter that sets the default values.
  • +
  • 'delimiter' - A one-character string used to separate fields.
  • +
  • 'quotechar' - Character for quoting fields that contain special characters.
  • +
  • 'doublequote' - Whether quotechars inside fields get doubled or escaped.
  • +
  • 'skipinitialspace' - Whether whitespace after delimiter gets stripped.
  • +
  • 'lineterminator' - How does writer terminate lines.
  • +
  • 'quoting' - Controls the amount of quoting: 0 - as necessary, 1 - all.
  • +
  • 'escapechar' - Character for escaping quotechar if doublequote is false.
  • +
+

Dialects

+
+------------------+--------+-----------+--------------+
+|                  | excel  | excel_tab | unix_dialect |
++------------------+--------+-----------+--------------+
+| delimiter        | ','    |   '\t'    |     ','      |
+| quotechar        | '"'    |   '"'     |     '"'      |
+| doublequote      | True   |   True    |     True     |
+| skipinitialspace | False  |   False   |     False    |
+| lineterminator   | '\r\n' |   '\r\n'  |     '\n'     |
+| quoting          | 0      |   0       |     1        |
+| escapechar       | None   |   None    |     None     |
++------------------+--------+-----------+--------------+
 

Read Rows from CSV File

def read_csv_file(filename):
     with open(filename, encoding='utf-8', newline='') as file:
-        return csv.reader(file, delimiter=';')
+        return csv.reader(file)
 
  • If 'newline=""' is not specified, then newlines embedded inside quoted fields will not be interpreted correctly.
  • @@ -1478,7 +1508,7 @@ os.replace(from, to) Write Rows to CSV File
    def write_to_csv_file(filename, rows):
         with open(filename, 'w', encoding='utf-8', newline='') as file:
    -        writer = csv.writer(file, delimiter=';')
    +        writer = csv.writer(file)
             writer.writerows(rows)
     

    #JSON

    @@ -1681,7 +1711,7 @@ lock.release() value = getattr(<object>, '<attr_name>') setattr(<object>, '<attr_name>', value) -

    Parameters

    +

    Parameters

    from inspect import signature
     <sig>        = signature(<function>)
     no_of_params = len(<sig>.parameters)
    @@ -1835,7 +1865,7 @@ pyplot.clf()  # Clears figure.
     from tabulate import tabulate
     import csv
     with open(<filename>, encoding='utf-8', newline='') as file:
    -    lines   = csv.reader(file, delimiter=';')
    +    lines   = csv.reader(file)
         headers = [header.title() for header in next(lines)]
         table   = tabulate(lines, headers)
         print(table)