Product
Introducing License Enforcement in Socket
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
:Author: Jan Vlcinsky :e-mail: jan.vlcinsky@gmail.com :license: BSD
ttr.xml.csv2xml module contains:
class Csv2Xml
Converter of CSV lines into xml elements or string
function string2xml
utility function to convert string into xml string in one shot.
Features:
read CSV into XML document
get headers from CSV and use them as element names
managing CSV format
on resulting xml define:
iterator over csv / xml rows
simple function string2xml including encoding
Python must be installed, version 2.6 is expected. You shall have access to Internet to installa additional packages automatically from PyPi_ (lxml at this moment) This package is not on PyPi_ yet (planned). Use of distribution package itself is needed at the moment and andy of following tools shall work
Using setup.py: unpack the package and run:: python setup.py install
Using easy_install:: easy_install <package_file>
Using pip:: pip install <package_file>
On windows, using binary setup.exe:: simply run the installation program.
Simple case could be: >>> from ttr.xml.csv2xml import string2xml >>> csv_str = """a;b;c ... 1;2;3 ... 11;22;33 ... 111;222;333""" >>> print string2xml(csv_str, delimiter = ";") 123112233111222333
If you like to specify output encoding (default is UTF-8), tell it by encoding parameter >>> print string2xml(csv_str, delimiter = ";", encoding = "windows-1250") 123112233111222333
The CSV format can be set also by registered dialects, adding also line numbering attribute >>> excel_str = """a,b,c ... 1,2,3 ... 11,22,33 ... 111,222,333""" >>> print string2xml(excel_str, dialect = "excel", row_num_att = "rownum") 123112233111222333
Or you can define your own dialect using csv.Dialect subclass >>> import csv >>> class DialectSemicolon(csv.Dialect): ... delimiter = ';' ... quotechar = '"' ... doublequote = True ... skipinitialspace = False ... lineterminator = '\r\n' ... quoting = csv.QUOTE_NONE ... >>> print string2xml(csv_str, dialect = DialectSemicolon) 123112233111222333
Csv2Xml provides only one type of source - file like object. It can be created by opening a file in style: f = open("my.csv", "b") or by using string buffers:
>>> from StringIO import StringIO
>>> buff = StringIO(csv_str)
>>> print buff # doctest:+ELLIPSIS
<StringIO.StringIO ...>
>>> from ttr.xml.csv2xml import Csv2Xml
>>> csv_convertor = Csv2Xml(buff, delimiter = ";") # doctest:+ELLIPSIS
>>> print csv_convertor.as_string()
<root><row><a>1</a><b>2</b><c>3</c></row><row><a>11</a><b>22</b><c>33</c></row><row><a>111</a><b>222</b><c>333</c></row></root>
This way you can easily manage conversion of files without any need to read them in advance into a string. You can specify CSV file format using the same methods as with string2xml function.
>>> buff2 = StringIO(excel_str)
>>> csv_convertor = Csv2Xml(buff2, dialect = "excel")
>>> print csv_convertor.as_string()
<root><row><a>1</a><b>2</b><c>3</c></row><row><a>11</a><b>22</b><c>33</c></row><row><a>111</a><b>222</b><c>333</c></row></root>
Instead of using string for storing resulting XML, more nataral is to get is as XML element You then get root element with all the row - related elements being nested inside The only difference is you call as_element method now:
>>> buff2 = StringIO(excel_str) # doctest: +ELLIPSIS
>>> csv_convertor = Csv2Xml(buff2, dialect = "excel")
>>> xml_elm = csv_convertor.as_element()
>>> print xml_elm # doctest: +ELLIPSIS
<Element root ...>
>>> from lxml import etree
>>> etree.tostring(xml_elm)
'<root><row><a>1</a><b>2</b><c>3</c></row><row><a>11</a><b>22</b><c>33</c></row><row><a>111</a><b>222</b><c>333</c></row></root>'
Csv2Xml converter can function also as an iterator - in such a case it will not return root element, but only the row elements.
>>> buff = StringIO(excel_str)
>>> csv_converter = Csv2Xml(buff, dialect = "excel")
>>> for xml_row in csv_converter:
... print etree.tostring(xml_row)
<row><a>1</a><b>2</b><c>3</c></row>
<row><a>11</a><b>22</b><c>33</c></row>
<row><a>111</a><b>222</b><c>333</c></row>
.. _PyPi: http://pypi.python.org/pypi
.. This is your project NEWS file which will contain the release notes. .. Example: http://www.python.org/download/releases/2.6/NEWS.txt .. The content of this file, along with README.rst, will appear in your .. project's PyPI page.
Release date: 2011-08-12
FAQs
module, providing conversion of csv file into xml
We found that ttr.xml.csv2xml demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Ensure open-source compliance with Socket’s License Enforcement Beta. Set up your License Policy and secure your software!
Product
We're launching a new set of license analysis and compliance features for analyzing, managing, and complying with licenses across a range of supported languages and ecosystems.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.