{ "info": { "author": "Kawamura Shintaro", "author_email": "kawasin73@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3" ], "description": "# nested_csv\n\n[![Actions Status](https://github.com/kawasin73/nested_csv/workflows/Python%20package/badge.svg)](https://github.com/kawasin73/nested_csv/actions)\n\nnested_csv generates CSV from nested `dict` `list` data structure such as JSON.\n\n`nested_csv.NestedDictWriter` have same interface (`writerow`, `writerows`, `writeheader`) with `csv.DictWriter`.\n\n## Install\n\n```bash\n$ pip install nested-csv\n```\n\n## Dependency\n\nnested_csv requires Python 3.\n\nnested_csv only use standard packages (`csv`, `itertools`, `re`).\n\n## How to Use\n\n### Estimate fields\n\n```python\nfrom nested_csv import generate_fieldnames\ndata = [\n {\"hello\": {\"world\": \"value0\"}, \"list\": [[1,2,3], [4,5,6]], \"fixed\": [1,2]},\n {\"hello\": {\"world\": \"value1\"}, \"list\": [[7,8], [10,11]], \"fixed\": [3,4]},\n]\nfieldnames = generate_fieldnames(data[0])\n# ['fixed[id]', 'fixed[]', 'hello.world', 'list[id]', 'list[][id]', 'list[][]']\n```\n\n### Generate Nested CSV\n\n```python\nimport io\nfrom nested_csv import NestedDictWriter\ndata = [\n {\"hello\": {\"world\": \"value0\"}, \"list\": [[1,2,3], [4,5,6]], \"fixed\": [1,2]},\n {\"hello\": {\"world\": \"value1\"}, \"list\": [[7,8], [10,11]], \"fixed\": [3,4]},\n]\nfieldnames = ['hello.world', 'list[id]', 'list[][id]', 'list[][]', 'fixed[1]']\nfile = io.StringIO()\nw = NestedDictWriter(file, fieldnames)\nw.writeheader()\nw.writerows(data)\nfile.seek(0)\nfile.read()\n# hello.world,list[id],list[][id],list[][],fixed[1]\n# value0,0,0,1,2\n# value0,0,1,2,2\n# value0,0,2,3,2\n# value0,1,0,4,2\n# value0,1,1,5,2\n# value0,1,2,6,2\n# value1,0,0,7,4\n# value1,0,1,8,4\n# value1,1,0,10,4\n# value1,1,1,11,4\n```\n\n## Field Format\n\nYou can generate fields format automatically by using `generate_fieldnames()`.\n\nBut `generate_fieldnames()` is sometimes not appropriate when ...\n\n- the order of csv row is important\n - `generate_fieldnames()` will generate fields ordered by lexical order\n- the base object includes length list\n - `generate_fieldnames()` can't estimate the scheme of empty object\n\nPlease set fieldnames manually on those situations.\n\nThe format of fieldnames is following.\n\n### nested keys\n\nNested dict key is joined by `.` (dot).\n\n```\nJSON : {\"a\": {\"b\": 1, \"c\": 2}}\nformat : [\"a.b\", \"a.c\"]\n--- CSV ---\n1,2\n```\n\n### id support\n\n`NestedDictWriter` will insert `id` passed on `writerow(rowdict, id=0)` or `writerows(rowdicts, first_id=0)`.\n\n```\nJSON : {\"a\": {\"b\": 1, \"c\": 2}}\nformat : [\"id\", \"a.b\", \"a.c\"]\n--- CSV ---\n0,1,2\n```\n\n### fixed size list\n\nWhen the size of the list is known, list elements can be specified by index `[]`.\n\n```\nJSON : {\"a\": [1,2], \"b\": [0]}\nformat : [\"a[0]\", \"a[1]\", \"b[0]\"]\n--- CSV ---\n1,2,0\n```\n\n### list\n\n`NestedDictWriter` unfolds the list elements into multiple rows when specified by `[]`.\n\nNested list is also supported.\n\nThe level of each list must match.\n\n- Bad Cases\n - `\"a[]\"` + `\"a.b\"` : key and list at same level\n - `\"a[]\"` + `\"b.c[]\"` : list level not match\n - `\"a[][]\"` + `\"b[].c[]\"` : sub list level not match\n\nThe count of unfolding loop is maximum length of list at each level. If list size is shorter than maximum, the value of exceeded index is empty.\n\n```\nJSON : {\"a\": [[{\"x\": 1}], [{\"x\": 2}, {\"x\": 3}]], \"b\": [4, 5, 6], \"c\": [[7, 8, 9]]}\nformat : [\"a[][].x\", \"b[]\", \"c[][]\"]\n--- csv ---\n1,4,7\n,4,8\n,4,9\n2,5,\n3,5,\n,5,\n,6,\n,6,\n,6,\n```\n\n### list index\n\nThe unfolding loop index of list is specified by `[id]`.\n\n```\nJSON: {\"a\": [[1,2,3], [4,5]}\nformat : [\"a[id]\", \"a[][id]\", \"a[][]\"]\n--- csv ---\n0,0,1\n0,1,2\n0,2,3\n1,0,4\n1,1,5\n1,2,\n```\n\n## LICENSE\n\nMIT\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/kawasin73/nested_csv", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "nested-csv", "package_url": "https://pypi.org/project/nested-csv/", "platform": "", "project_url": "https://pypi.org/project/nested-csv/", "project_urls": { "Homepage": "https://github.com/kawasin73/nested_csv" }, "release_url": "https://pypi.org/project/nested-csv/0.1.0/", "requires_dist": null, "requires_python": ">=3.6", "summary": "CSV generator for nested dict or list data structure such asJSON", "version": "0.1.0", "yanked": false, "yanked_reason": null }, "last_serial": 6038024, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "e10d43bba33ba4508ab8ca6d2be13536", "sha256": "37749e6b95ed840c8e1a1fc4ac6649e22282c060da6d6af5a41af570063a1829" }, "downloads": -1, "filename": "nested_csv-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e10d43bba33ba4508ab8ca6d2be13536", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 5968, "upload_time": "2019-10-27T19:01:43", "upload_time_iso_8601": "2019-10-27T19:01:43.806256Z", "url": "https://files.pythonhosted.org/packages/c2/09/7d9c151c94857081addb7d26402b59ceb47486d926fe9e5df3f0b8071637/nested_csv-0.1.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "b2e0d8b5fe387a1ad672ff68ae14fd2c", "sha256": "0cd1f162af4a635d1f3e0b36cea3f9396422aa0c72684b53ed1a6a6197a1a6f5" }, "downloads": -1, "filename": "nested_csv-0.1.0.tar.gz", "has_sig": false, "md5_digest": "b2e0d8b5fe387a1ad672ff68ae14fd2c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 5703, "upload_time": "2019-10-27T19:01:46", "upload_time_iso_8601": "2019-10-27T19:01:46.623506Z", "url": "https://files.pythonhosted.org/packages/2d/e1/c77ab8c5d76ddf511de9aef4dfd74a3e3c4c9ea6967cf312019d0fa11519/nested_csv-0.1.0.tar.gz", "yanked": false, "yanked_reason": null } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "e10d43bba33ba4508ab8ca6d2be13536", "sha256": "37749e6b95ed840c8e1a1fc4ac6649e22282c060da6d6af5a41af570063a1829" }, "downloads": -1, "filename": "nested_csv-0.1.0-py3-none-any.whl", "has_sig": false, "md5_digest": "e10d43bba33ba4508ab8ca6d2be13536", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": ">=3.6", "size": 5968, "upload_time": "2019-10-27T19:01:43", "upload_time_iso_8601": "2019-10-27T19:01:43.806256Z", "url": "https://files.pythonhosted.org/packages/c2/09/7d9c151c94857081addb7d26402b59ceb47486d926fe9e5df3f0b8071637/nested_csv-0.1.0-py3-none-any.whl", "yanked": false, "yanked_reason": null }, { "comment_text": "", "digests": { "md5": "b2e0d8b5fe387a1ad672ff68ae14fd2c", "sha256": "0cd1f162af4a635d1f3e0b36cea3f9396422aa0c72684b53ed1a6a6197a1a6f5" }, "downloads": -1, "filename": "nested_csv-0.1.0.tar.gz", "has_sig": false, "md5_digest": "b2e0d8b5fe387a1ad672ff68ae14fd2c", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.6", "size": 5703, "upload_time": "2019-10-27T19:01:46", "upload_time_iso_8601": "2019-10-27T19:01:46.623506Z", "url": "https://files.pythonhosted.org/packages/2d/e1/c77ab8c5d76ddf511de9aef4dfd74a3e3c4c9ea6967cf312019d0fa11519/nested_csv-0.1.0.tar.gz", "yanked": false, "yanked_reason": null } ], "vulnerabilities": [] }