Skip to main content

Parse/serialize any markup format

Project description

Parse or serialize any markup. Currently supports ini, json, xml and yaml. Report bugs and new functionality requests at https://github.com/bkabrda/anymarkup/issues.

Parsing:

import anymarkup
anymarkup.parse('foo: bar')
anymarkup.parse_file('foo/bar.ini')

Serializing:

import anymarkup
anymarkup.serialize({'foo': 'bar'}, 'json')
anymarkup.serialize_file({'foo': 'bar'}, 'path/to/file.json')

anymarkup is licensed under BSD license. You can download official releases from https://pypi-hypernode.com/pypi/anymarkup or install them via pip install anymarkup.

anymarkup works with Python 2.7 and >= 3.3.

Notes on Parsing Basic Types

When parsing, anymarkup recognizes basic types - NoneType, int, float and bool (and long on Python 2) and converts all values to these types. If you want to get everything as strings, just use force_types=False with parse or parse_file:

import anymarkup
# will yield {'a': 1}
anymarkup.parse('a: 1')
# will yield {'a': '1'}
anymarkup.parse('a: 1', force_types=False)

Notes on OrderedDict

Parsing certain types of markup can yield Python’s OrderedDict type - namely XML documents and YAML !!omap (see http://yaml.org/type/omap.html). anymarkup handles this without a problem, but note that if you serialize these as JSON or INI and then parse again, you’ll lose the ordering information (meaning you’ll get just dict back).

This is because JSON and INI parsers (to my knowledge) don’t consider ordering key-value structures important and there’s no direct means in these markup languages to express ordering key-value structures.

Examples

Parsing examples:

import anymarkup

ini = """
[a]
foo = bar"""

json = """
{"a": {
    "foo": "bar"
}}"""

xml = """<?xml version="1.0" encoding="UTF-8"?>
<a>
    <foo>bar</foo>
</a>"""

yaml = """
a:
  foo: bar
"""

# these will all yield the same value (except that xml parsing will yield OrderedDict)
anymarkup.parse(ini)
anymarkup.parse(json)
anymarkup.parse(xml)
anymarkup.parse(yaml)

# explicitly specify a type of format to expect and/or encoding (utf-8 is default)
anymarkup.parse('foo: bar', format='yaml', encoding='ascii')

# by default, anymarkup recognizes basic types (None, booleans, ints and floats)
#   if you want to get everything as strings, just use force_types=False

# will yield {'a': 1, 'b': True, 'c': None}
anymarkup.parse('a: 1\nb: True\nc: None')
# will yield {'a': '1', 'b': 'True', 'c': 'None'}
anymarkup.parse('a: 1\nb: True\nc: None', force_types=False)

# or parse a file
anymarkup.parse_file('foo.ini')

# if a file doesn't have a format extension, pass it explicitly
anymarkup.parse_file('foo', format='json')

# you can also pass encoding explicitly (utf-8 is default)
anymarkup.parse_file('bar', format='xml', encoding='ascii')

Serializing examples:

import anymarkup

struct = {'a': ['b', 'c']}

for fmt in ['ini', 'json', 'xml', 'yaml']:
    # any of the above formats can be used for serializing
    anymarkup.serialize(struct, fmt)

# explicitly specify encoding (utf-8 is default)
anymarkup.serialize(struct, 'json', encoding='utf-8')

# or serialize directly to a file
anymarkup.serialize_file(struct, 'foo/bar.ini')

# if a file doesn't have a format extension, pass it explicitly
anymarkup.serialize_file(struct, 'foo/bar', format='json')

# you can also pass encoding explicitly (utf-8 is default)
anymarkup.serialize_file(struct, 'foo/bar', format='json', encoding='ascii')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anymarkup-0.3.1.tar.gz (10.5 kB view details)

Uploaded Source

File details

Details for the file anymarkup-0.3.1.tar.gz.

File metadata

  • Download URL: anymarkup-0.3.1.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for anymarkup-0.3.1.tar.gz
Algorithm Hash digest
SHA256 311268eae70f211ae4f34927f1f75c3a3ee7096a697d4b9b30d5e7a52ee2f61e
MD5 9fd75f6f31975a8a65ecb5c9f1e5f996
BLAKE2b-256 f4b6e99ea6b37f7aa80f5ebd0853b33c6090eb7599203569ca899e4149d87cb6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page