Edit page in Livemark
(2022-09-19 18:33)

Csv Format

CSV is a file format which you can you in Frictionless for reading and writing. Arguable it's the main Open Data format so it's supported very well in Frictionless.

Reading Data

You can read this format using Package/Resource, for example:

from pprint import pprint
from frictionless import Resource

resource = Resource('table.csv')
pprint(resource.read_rows())
[{'id': 1, 'name': 'english'}, {'id': 2, 'name': '中国人'}]

Writing Data

The same is actual for writing:

from frictionless import Resource

source = Resource(data=[['id', 'name'], [1, 'english'], [2, 'german']])
target = source.write('table-output.csv')
print(target)
print(target.to_view())
{'name': 'table-output',
 'type': 'table',
 'path': 'table-output.csv',
 'scheme': 'file',
 'format': 'csv',
 'mediatype': 'text/csv'}
+----+-----------+
| id | name      |
+====+===========+
|  1 | 'english' |
+----+-----------+
|  2 | 'german'  |
+----+-----------+

Configuration

There is a control to configure how Frictionless read and write files in this format. For example:

from frictionless import Resource, formats

resource = Resource(data=[['id', 'name'], [1, 'english'], [2, 'german']])
resource.write('tmp/table.csv', control=formats.CsvControl(delimiter=';'))

Reference

formats.CsvControl (class)

formats.CsvControl (class)

Csv dialect representation

Signature

(*, title: Optional[str] = None, description: Optional[str] = None, delimiter: str = ,, line_terminator: str = \r\n, quote_char: str = ", double_quote: bool = True, escape_char: Optional[str] = None, null_sequence: Optional[str] = None, skip_initial_space: bool = False, comment_char: Optional[str] = None) -> None

Parameters

  • title (Optional[str])
  • description (Optional[str])
  • delimiter (str)
  • line_terminator (str)
  • quote_char (str)
  • double_quote (bool)
  • escape_char (Optional[str])
  • null_sequence (Optional[str])
  • skip_initial_space (bool)
  • comment_char (Optional[str])

formats.csvControl.delimiter (property)

NOTE: add docs

Signature

str

formats.csvControl.line_terminator (property)

NOTE: add docs

Signature

str

formats.csvControl.quote_char (property)

NOTE: add docs

Signature

str

formats.csvControl.double_quote (property)

NOTE: add docs

Signature

bool

formats.csvControl.escape_char (property)

NOTE: add docs

Signature

Optional[str]

formats.csvControl.null_sequence (property)

NOTE: add docs

Signature

Optional[str]

formats.csvControl.skip_initial_space (property)

NOTE: add docs

Signature

bool

formats.csvControl.comment_char (property)

NOTE: add docs

Signature

Optional[str]

formats.csvControl.to_python (method)

Conver to Python's `csv.Dialect`

It's a beta version of Frictionless Framework (v5). Read Frictionless Framework (v4) docs for a version that is currently installed by default by pip.