Skip to content

Commit

Permalink
add pandas example to readme; fix read function return type; update v…
Browse files Browse the repository at this point in the history
…ersion
  • Loading branch information
OlivierBinette committed Nov 26, 2023
1 parent 728a848 commit a88ac26
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand Down Expand Up @@ -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)
Expand All @@ -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
2 changes: 1 addition & 1 deletion csvmeta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion csvmeta/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]]:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "[email protected]" }]
readme = "README.md"
Expand Down

0 comments on commit a88ac26

Please sign in to comment.