Seekeer
Seekeer

Reputation: 1394

Serialize collection to parse in python

I have a collection of simple elements in C#. Element is like:

public class Element
{
    public string AName { get; private set; }

    public string PName { get; private set; }

    public string Value { get; set; }
}

I need to pass this collection as a string to a Python script. And in python for each element I need to call function like Func(AName, PName, Value). I've done this by serializing collection to JSON in C# and in python write code:

elements = json.loads(JSON)
for element in elements:
    Func(routeLayer, element['AName'], element['PName'], element['Value'])

But now it turns out that I cannot use Python's json module. So I need a new way to pass this collection to script and dont use any additional modules. I am a real noob in Python, so solutions I can imagine are ugly. Can you give me an advice?

Upvotes: 0

Views: 733

Answers (2)

unutbu
unutbu

Reputation: 880449

If there is some character which you can guarantee is not in the strings for AName,PName and Value then you could use that character as a separator. In C#, you could "serialize" the information by simply joining the three strings with the separator, e.g. "foo,bar,baz".

Then in Python, the information could be deserialized with

aName,pName,value = element.split(',')

PS. Just out of curiosity, why can't you import modules in the standard library?

Upvotes: 1

Alexander Yezutov
Alexander Yezutov

Reputation: 3214

The most common standard, which should be available in Python as well is XML. Did you consider this option?

UPDATE: You can use XmlSerializer in C# to serialize as XML. And here is something, what i've found for Python

Upvotes: 0

Related Questions