{ "info": { "author": "Maurits van Rees", "author_email": "m.van.rees@zestsoftware.nl", "bugtrack_url": null, "classifiers": [ "Programming Language :: Python" ], "description": ".. contents::\n\n\nDocumentation\n=============\n\n\nWhat is this?\n-------------\n\nThis package defines base classes ``BaseCSVReader`` and\n``BaseMultilineCSVReader``. These can be used to iterate over a csv\nfile and return its contents as a dictionary. Normally you should use\nthe ``BaseCSVReader``. The ``BaseMultilineCSVReader`` can be used\nwhen you run into problems with csv files that can have newline\ncharacters within a column, which could trip up the standard reader.\n\n\nExample usage\n-------------\n\nYou should write an own class that inherits from one of the base\nclasses. The ``example.py`` file has an example. Basically it\nwill be something like this::\n\n from collective.csv2dict import BaseCSVReader, to_int, to_string\n\n class ExampleCSVReader(BaseCSVReader):\n \"\"\"Example csv reader class.\n\n We read three columns and skip one.\n \"\"\"\n skip = [2] # skip column index 2\n fields = [\n # The format is: (field name, filter method)\n ('id', to_int),\n ('fullname', to_string),\n ('email', to_string),\n ]\n\nYou can then use this class to read a csv file. The ``example.py``\nfile again has sample code to read the csv file and some options from\nthe command line. Simply put, it boils down to this::\n\n c = reader(open(filename, 'U'))\n # Iterate over the entries and print them.\n for entry in c:\n print entry\n print '%d entries ignored due to errors.' % c.ignored\n print '%d entries read without errors.' % c.success\n\nIt would turn this csv (contained in ``example.csv``)::\n\n 1,Maurits van Rees,ignored,maurits@example.org\n 2,Arthur Dent,ignored again,dentarthurdent@example.org\n\ninto this dictionary::\n\n {'email': u'maurits@example.org',\n 'fullname': u'Maurits van Rees',\n 'id': 1}\n {'email': u'dentarthurdent@example.org',\n 'fullname': u'Arthur Dent',\n 'id': 2}\n\n\nNotes\n-----\n\n- It is recommented to always open a file in universal newline mode.\n This is usually the best way to avoid some potential problems with\n newlines within a single row.\n\n- The base reader tries to guess the encoding of the file in a\n simplistic way and will avoid breaking when no good encoding can be\n found.\n\n- The reader might ignore the first row of the csv file as it may be a\n header. We do a simple check for this: if none of the columns of\n the first row can be turned into an integer, then it is not a header\n line and it will be treated as data. If this logic does not work\n for you, then override the ``is_header`` method in your own class,\n simply like this::\n\n def is_header(self, items):\n return False\n\n That will make sure the first line is always treated as data. If\n you want it to always be treated as a header, just do ``return\n True``.\n\n- You can override the ``prepare_iterable`` method if you need to\n do some fixes to some rows or the complete csv file before the\n reader starts to handle it. The ``BaseMultilineCSVReader`` has an\n example for this.\n\n- By default the excel csv dialect is used (or whatever your Python\n version has as default). If you want to use a specific dialect, you\n can override the ``dialect`` variable in your reader class. For\n example, you can use tabs as delimiter like this::\n\n import csv\n\n class MyDialect(csv.excel):\n delimiter = '\\t'\n\n csv.register_dialect('mydialect', MyDialect)\n\n class ExampleCSVReader(BaseCSVReader):\n dialect = 'mydialect'\n fields = [...]\n\n\nCompatibility\n-------------\n\nI have tried this on Python 2.6 and an earlier version on 2.4. It\nwill likely work on all 2.x versions from 2.3 onwards.\n\nTested on Mac OS X so likely also working on any Unix-like system.\nShould work on Windows too, though I can imagine problems with newline\ncharacters in some corner cases.\n\n\nNote for Plone users\n--------------------\n\nI usually make packages for use in Plone, but this one can be used\nwith plain Python. Nevertheless, a note for Plone users is probably\ngood.\n\nIf you want to use it within your Plone buildout, just add it to the\neggs in your buildout.cfg. You do not need to load zcml or install\nanything. You just need to write your own class definition, as in the\nexample above. Then you probably want to write a browser view that\nuses this class to turn some uploaded csv file to a dictionary. Then\nyou probably create a content item or a member for each item in this\ndictionary or do whatever you want with it.\n\n\nAuthors\n-------\n\n- Maurits van Rees (package creation, various improvements and\n generalizations)\n\n- Guido Wesdorp (initial code, written for a client way back in 2007)\n\nChangelog\n=========\n\n\n1.1 (2014-04-11)\n----------------\n\n- Optionally allow ignoring extra columns. To use this: initialize\n the reader with ``ignore_extra_columns=True``.\n [maurits]\n\n- Add ``formatting`` method to readers. It currently returns the\n delimiter, the dialect instance, the encoding and the expected\n number of columns. You can use this to give a hint in an upload\n form.\n [maurits]\n\n\n1.0 (2012-06-21)\n----------------\n\n- Initial release\n [maurits]", "description_content_type": null, "docs_url": null, "download_url": "UNKNOWN", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/collective/collective.csv2dict", "keywords": "", "license": "GPL", "maintainer": null, "maintainer_email": null, "name": "collective.csv2dict", "package_url": "https://pypi.org/project/collective.csv2dict/", "platform": "UNKNOWN", "project_url": "https://pypi.org/project/collective.csv2dict/", "project_urls": { "Download": "UNKNOWN", "Homepage": "https://github.com/collective/collective.csv2dict" }, "release_url": "https://pypi.org/project/collective.csv2dict/1.1/", "requires_dist": null, "requires_python": null, "summary": "Turn a csv into a dictionary with a predefined schema.", "version": "1.1" }, "last_serial": 1057295, "releases": { "1.0": [ { "comment_text": "", "digests": { "md5": "ed36ce5e6353ea2e7fa16d123e477d8b", "sha256": "66b0433aae6211c5a868662a423c29f7c2b9667ccf29fdd9a2cd1c4eb594be61" }, "downloads": -1, "filename": "collective.csv2dict-1.0.zip", "has_sig": false, "md5_digest": "ed36ce5e6353ea2e7fa16d123e477d8b", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 23435, "upload_time": "2012-06-21T18:31:18", "url": "https://files.pythonhosted.org/packages/09/43/8379e20d688c2afa7bdf6f39fcef1ab6addee70ef1997bcd787e42808521/collective.csv2dict-1.0.zip" } ], "1.1": [ { "comment_text": "", "digests": { "md5": "de31d914e764ade56969cbf76b228e46", "sha256": "b953d57e5ca3afcdec9793231d66bb92242875348d615d2b290dce6700218e69" }, "downloads": -1, "filename": "collective.csv2dict-1.1.zip", "has_sig": false, "md5_digest": "de31d914e764ade56969cbf76b228e46", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24330, "upload_time": "2014-04-11T00:19:46", "url": "https://files.pythonhosted.org/packages/ca/18/21b9d976478a9d4b88a8a05980f7d715503876a5e6ea574866e9f1432bdc/collective.csv2dict-1.1.zip" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "de31d914e764ade56969cbf76b228e46", "sha256": "b953d57e5ca3afcdec9793231d66bb92242875348d615d2b290dce6700218e69" }, "downloads": -1, "filename": "collective.csv2dict-1.1.zip", "has_sig": false, "md5_digest": "de31d914e764ade56969cbf76b228e46", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 24330, "upload_time": "2014-04-11T00:19:46", "url": "https://files.pythonhosted.org/packages/ca/18/21b9d976478a9d4b88a8a05980f7d715503876a5e6ea574866e9f1432bdc/collective.csv2dict-1.1.zip" } ] }