From a88ac26ea12162116a1ada329985919f0b9c8ebb Mon Sep 17 00:00:00 2001 From: Olivier Binette Date: Sat, 25 Nov 2023 19:46:12 -0500 Subject: [PATCH] add pandas example to readme; fix read function return type; update version --- README.md | 33 ++++++++++++++++++++++++++++++++- csvmeta/__init__.py | 2 +- csvmeta/read.py | 2 +- pyproject.toml | 2 +- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1674a66..de15c63 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,6 @@ csvm.write('mydata.csv', data) # Read data from a csv file folder data = csvm.read('mydata.csv') -list(data) ## [ ## ['name', 'age', 'state'], ## ['Nicole', '43', 'CA'], @@ -104,6 +103,34 @@ csvm.metadata('mydata.csv') ## } ``` +### Reading to Pandas DataFrame + +```python +import csvmeta as csvm + +data = [ + ['name', 'age', 'state'], + ['Nicole', 43, 'CA'], + ['John', 28, 'DC'] +] + +# Write data and metadata to a csv file folder +csvm.write('mydata.csv', data, header=True) + + +data = csvm.read('mydata.csv') +metadata = csvm.metadata('mydata.csv') +if metadata.get("header", False): + df = pd.DataFrame(data[1:], columns=data[0]) +else: + df = pd.DataFrame(data) + +df +## name age state +## 0 Nicole 43 CA +## 1 John 28 DC +``` + ## Links and References - [CSV Module Documentation](https://docs.python.org/3/library/csv.html) @@ -114,6 +141,10 @@ csvm.metadata('mydata.csv') ## Changelog +### 1.1.0 (2023-11-25) + +- Fix read function return type: now return list of lists instead of generator + ### 1.0.0 (2023-11-25) - Initial release diff --git a/csvmeta/__init__.py b/csvmeta/__init__.py index d30ec7e..e3db2e0 100644 --- a/csvmeta/__init__.py +++ b/csvmeta/__init__.py @@ -2,4 +2,4 @@ from csvmeta.write import DEFAULT_DIALECT, write __all__ = ["read", "metadata", "write", "DEFAULT_DIALECT"] -__version__ = "1.0.0" +__version__ = "1.1.0" diff --git a/csvmeta/read.py b/csvmeta/read.py index 187ad43..21ce8fc 100644 --- a/csvmeta/read.py +++ b/csvmeta/read.py @@ -27,7 +27,7 @@ def read(dirpath: str) -> Iterable[Iterable[str]]: reader = _read_csv(dirpath, dialect) - return reader + return list(reader) def _read_csv(dirpath: str, dialect: Union[str, dict]) -> Iterable[Iterable[str]]: diff --git a/pyproject.toml b/pyproject.toml index 9635c28..874e1c1 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta" [project] name = "csvmeta" -version = "1.0.0" +version = "1.1.0" description = "Lightweight csv read/write, keeping track of csv dialect and other metadata." authors = [{ name = "Olivier Binette", email = "olivier@olivierbinette.ca" }] readme = "README.md"