How to read a Python marshal-written file in C#?
up vote
1
down vote
favorite
I'm working on converting a markov-chain chatbot from Python to C# for some friends, but am having a bit of difficulty with a language feature of Python that I have not encountered before. The bot is built off of pyborg, https://github.com/bdrewery/PyBorg.
Part of the initialization of this bot is reading from a words.dat and lines.dat file to populate dictionaries, like so:
content = self.read_file("words.dat")
self.words = marshal.loads(content)
del content
content = self.read_file("lines.dat")
self.lines = marshal.loads(content)
del content
Both of these files are full of the results of a call to "marshal.dumps" with large dictionary objects passed in as the arguments like so:
f = open("words.dat", "wb")
s = marshal.dumps(self.words)
f.write(s)
f.close()
f = open("lines.dat", "wb")
s = marshal.dumps(self.lines)
f.write(s)
f.close()
From what I've been able to gather, the Python "marshal" module is responsible for a type of serialization, in which objects are converted into a byte representation "specific to Python." https://docs.python.org/2/library/marshal.html
I expect these files to be full of key/value sets, but I haven't been able to find any information on how to convert from this "marshal" byte set to something I can parse as string, List, or Dictionary objects in C#. Is there a way to do so without executing external Python scripts from my application?
c# python serialization marshalling
add a comment |
up vote
1
down vote
favorite
I'm working on converting a markov-chain chatbot from Python to C# for some friends, but am having a bit of difficulty with a language feature of Python that I have not encountered before. The bot is built off of pyborg, https://github.com/bdrewery/PyBorg.
Part of the initialization of this bot is reading from a words.dat and lines.dat file to populate dictionaries, like so:
content = self.read_file("words.dat")
self.words = marshal.loads(content)
del content
content = self.read_file("lines.dat")
self.lines = marshal.loads(content)
del content
Both of these files are full of the results of a call to "marshal.dumps" with large dictionary objects passed in as the arguments like so:
f = open("words.dat", "wb")
s = marshal.dumps(self.words)
f.write(s)
f.close()
f = open("lines.dat", "wb")
s = marshal.dumps(self.lines)
f.write(s)
f.close()
From what I've been able to gather, the Python "marshal" module is responsible for a type of serialization, in which objects are converted into a byte representation "specific to Python." https://docs.python.org/2/library/marshal.html
I expect these files to be full of key/value sets, but I haven't been able to find any information on how to convert from this "marshal" byte set to something I can parse as string, List, or Dictionary objects in C#. Is there a way to do so without executing external Python scripts from my application?
c# python serialization marshalling
1
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working on converting a markov-chain chatbot from Python to C# for some friends, but am having a bit of difficulty with a language feature of Python that I have not encountered before. The bot is built off of pyborg, https://github.com/bdrewery/PyBorg.
Part of the initialization of this bot is reading from a words.dat and lines.dat file to populate dictionaries, like so:
content = self.read_file("words.dat")
self.words = marshal.loads(content)
del content
content = self.read_file("lines.dat")
self.lines = marshal.loads(content)
del content
Both of these files are full of the results of a call to "marshal.dumps" with large dictionary objects passed in as the arguments like so:
f = open("words.dat", "wb")
s = marshal.dumps(self.words)
f.write(s)
f.close()
f = open("lines.dat", "wb")
s = marshal.dumps(self.lines)
f.write(s)
f.close()
From what I've been able to gather, the Python "marshal" module is responsible for a type of serialization, in which objects are converted into a byte representation "specific to Python." https://docs.python.org/2/library/marshal.html
I expect these files to be full of key/value sets, but I haven't been able to find any information on how to convert from this "marshal" byte set to something I can parse as string, List, or Dictionary objects in C#. Is there a way to do so without executing external Python scripts from my application?
c# python serialization marshalling
I'm working on converting a markov-chain chatbot from Python to C# for some friends, but am having a bit of difficulty with a language feature of Python that I have not encountered before. The bot is built off of pyborg, https://github.com/bdrewery/PyBorg.
Part of the initialization of this bot is reading from a words.dat and lines.dat file to populate dictionaries, like so:
content = self.read_file("words.dat")
self.words = marshal.loads(content)
del content
content = self.read_file("lines.dat")
self.lines = marshal.loads(content)
del content
Both of these files are full of the results of a call to "marshal.dumps" with large dictionary objects passed in as the arguments like so:
f = open("words.dat", "wb")
s = marshal.dumps(self.words)
f.write(s)
f.close()
f = open("lines.dat", "wb")
s = marshal.dumps(self.lines)
f.write(s)
f.close()
From what I've been able to gather, the Python "marshal" module is responsible for a type of serialization, in which objects are converted into a byte representation "specific to Python." https://docs.python.org/2/library/marshal.html
I expect these files to be full of key/value sets, but I haven't been able to find any information on how to convert from this "marshal" byte set to something I can parse as string, List, or Dictionary objects in C#. Is there a way to do so without executing external Python scripts from my application?
c# python serialization marshalling
c# python serialization marshalling
asked Nov 12 at 5:10
JohnR
61
61
1
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20
add a comment |
1
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20
1
1
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256247%2fhow-to-read-a-python-marshal-written-file-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53256247%2fhow-to-read-a-python-marshal-written-file-in-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The marshal module is not designed to be parsed by external tools as stated in the docs. Choosing it for persistence was IMHO a bad design choice of the authors. You might be able to convert the files to something more useful using a Python script but outside of the Python world it will be next to impossible to retrieve the data. Even with the wrong Python version you will have troubles.
– Klaus D.
Nov 12 at 5:45
What I'd do is write my own load and dump routines in c# for a fairly standard / understandable format, e.g. csv or fixed width. Then write a simple script in python to load and marshal the source / input files (as per the code above) and then save them in your .csv format. Then you can ditch the marshal'd files completely.
– GregHNZ
Nov 12 at 5:56
Yeah, I definitely agree that continuing to rely on the marshal module is a recipe for disaster. I'll try to extract and serialize the files as something more C#-friendly with Python, that seems a much more approachable path than getting C# to correctly parse the current .dats. Thanks guys.
– JohnR
Nov 12 at 16:20