recode

This module gathers operations for text recoding

list_to_multilinestring

class textops.list_to_multilinestring(in_place=False, tag='-------------------------< Multiline string as list >-------------------------')

In a data structure, change all tagged list of strings into multiline strings

This is useful to undo data recoded by multilinestring_to_list.

Returns:Same data structure with tagged lists replaced by multiple line strings.

Examples

>>> data=[
...    [
...        "-------------------------< Multiline string as list >-------------------------",
...        "line1",
...        "line2",
...        "line3"
...    ],
...    {
...        "key2": [
...            "-------------------------< Multiline string as list >-------------------------",
...            "lineA",
...            "lineB",
...            "lineC",
...            "lineD"
...        ],
...        "key1": "one line"
...    }
... ]
>>> data | list_to_multilinestring()
['line1\nline2\nline3', {'key2': 'lineA\nlineB\nlineC\nlineD', 'key1': 'one line'}]

multilinestring_to_list

class textops.multilinestring_to_list(in_place=False)

In a data structure, change all multiline strings into a list of strings

This is useful for json.dump() or dumps() in order to have a readable json data when the structure has some strings with many lines. This works on imbricated dict and list. Dictionary keys are not changed, only their values. Each generated list of strings is tagged with a first item (MULTIPLELINESTRING_TAG). By this way the process is reversible : see list_to_multilinestring

Returns:Same data structure with multiple line strings replaced by lists.

Examples

>>> data=['line1\nline2\nline3',{'key1':'one line','key2':'lineA\nlineB\nlineC\nlineD'}]
>>> print(json.dumps(data,indent=4))  #doctest: +NORMALIZE_WHITESPACE
[
    "line1\nline2\nline3",
    {
        "key1": "one line",
        "key2": "lineA\nlineB\nlineC\nlineD"
    }
]
>>> print(json.dumps(data | multilinestring_to_list(),indent=4) )  #doctest: +NORMALIZE_WHITESPACE
[
    [
        "-------------------------< Multiline string as list >-------------------------",
        "line1",
        "line2",
        "line3"
    ],
    {
        "key1": "one line",
        "key2": [
            "-------------------------< Multiline string as list >-------------------------",
            "lineA",
            "lineB",
            "lineC",
            "lineD"
        ]
    }
]