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?










share|improve this question

















  • 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














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?










share|improve this question

















  • 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












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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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












  • 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

















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
);



);













draft saved

draft discarded


















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















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党