14.2.17. crate_anon.common.spreadsheet
crate_anon/common/spreadsheet.py
Copyright (C) 2015, University of Cambridge, Department of Psychiatry. Created by Rudolf Cardinal (rnc1001@cam.ac.uk).
This file is part of CRATE.
CRATE is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
CRATE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with CRATE. If not, see <https://www.gnu.org/licenses/>.
Functions for reading/writing spreadsheets.
- crate_anon.common.spreadsheet.gen_rows_from_csv(filename: str) Iterable[Sequence[Any]] [source]
Generates rows from a CSV file.
- crate_anon.common.spreadsheet.gen_rows_from_ods(filename: str) Iterable[Sequence[Any]] [source]
Generates rows from an ODS file, reading the first sheet.
- crate_anon.common.spreadsheet.gen_rows_from_spreadsheet(filename: str) Iterable[Sequence[Any]] [source]
Generates rows from a spreadsheet-type file, autodetecting it.
- Parameters:
filename – Filename to read.
- crate_anon.common.spreadsheet.gen_rows_from_tsv(filename: str) Iterable[Sequence[Any]] [source]
Generates rows from a TSV file.
- crate_anon.common.spreadsheet.gen_rows_from_xlsx(filename: str) Iterable[Sequence[Any]] [source]
Generates rows from an XLSX file, reading the first sheet.
- crate_anon.common.spreadsheet.make_safe_for_spreadsheet(x: Any) Any [source]
Helper function for
remove_none_values_from_spreadsheet()
.
- crate_anon.common.spreadsheet.remove_none_values_from_spreadsheet(data: Dict[str, Iterable[Sequence[Any]]]) Dict[str, Iterable[Sequence[Any]]] [source]
The ODS writer does not cope with
None
values, giving:AttributeError: 'NoneType' object has no attribute 'split'
Here, we transform
None
values to the empty string.
- crate_anon.common.spreadsheet.skip_spreadsheet_row(row: Sequence[Any]) bool [source]
Should we skip a row, because it’s empty or starts with a comment?
- crate_anon.common.spreadsheet.write_csv(filename: str, rows: Iterable[Sequence[Any]]) None [source]
Writes to a comma-separated values (CSV) file.
Empty (null) values are translated to “”.
- Parameters:
rows – Rows to write. (The first row is often a header row.)
filename – Name of file to write.
- crate_anon.common.spreadsheet.write_ods(filename: str, data: Dict[str, Iterable[Sequence[Any]]]) None [source]
Writes to an OpenOffice spreadsheet (ODS) file.
- Parameters:
data – See
write_spreadsheet()
.filename – Name of file to write.
- crate_anon.common.spreadsheet.write_spreadsheet(filename: str, data: Dict[str, Iterable[Sequence[Any]]], filetype: str | None = None) None [source]
Writes to a spreadsheet-style file, autodetecting it.
- Parameters:
filename – Name of file to write, or “-” for stdout (in which case the filetype is forced to TSV).
data – A dictionary whose keys are spreadsheet names and whose corresponding values contain spreadsheet data. (For TSV, which is a single-sheet format, only the first value is used.) Each dictionary value is an iterable containing rows, and each row is an iterable of cell data items.
filetype – File type as one of the string values of SpreadsheetFileExtensions; alternatively, use
None
to autodetect from the filename.
- crate_anon.common.spreadsheet.write_tsv(filename: str, rows: Iterable[Sequence[Any]]) None [source]
Writes to a tab-separated values (TSV) file.
Empty (null) values are translated to “”.
- Parameters:
rows – Rows to write. (The first row is often a header row.)
filename – Name of file to write.
- crate_anon.common.spreadsheet.write_xlsx(filename: str, data: Dict[str, Iterable[Sequence[Any]]]) None [source]
Writes to an OpenOffice spreadsheet (ODS) file.
- Parameters:
data – See
write_spreadsheet()
.filename – Name of file to write.