How to separate information in binary files in python












0















I'm just starting to learn how to work with python binary files, and i'm trying to write information into a file in binary mode. Since apparently there's no newline command for bytes (like n for strings), what is the easiest way to keep different chunks (of different sizes) of information separated in a binary file?
Ideally i would like to have different lines for it, but i cannot find a way to do so.










share|improve this question























  • You have to make your own encoding.

    – user202729
    Nov 21 '18 at 15:36











  • When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

    – Patrick Haugh
    Nov 21 '18 at 15:39











  • you mean like defining a custom set of characters to mark a division in my information?

    – Andres Calvo
    Nov 21 '18 at 15:43











  • You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

    – progmatico
    Nov 21 '18 at 16:03













  • With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

    – progmatico
    Nov 21 '18 at 16:07
















0















I'm just starting to learn how to work with python binary files, and i'm trying to write information into a file in binary mode. Since apparently there's no newline command for bytes (like n for strings), what is the easiest way to keep different chunks (of different sizes) of information separated in a binary file?
Ideally i would like to have different lines for it, but i cannot find a way to do so.










share|improve this question























  • You have to make your own encoding.

    – user202729
    Nov 21 '18 at 15:36











  • When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

    – Patrick Haugh
    Nov 21 '18 at 15:39











  • you mean like defining a custom set of characters to mark a division in my information?

    – Andres Calvo
    Nov 21 '18 at 15:43











  • You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

    – progmatico
    Nov 21 '18 at 16:03













  • With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

    – progmatico
    Nov 21 '18 at 16:07














0












0








0








I'm just starting to learn how to work with python binary files, and i'm trying to write information into a file in binary mode. Since apparently there's no newline command for bytes (like n for strings), what is the easiest way to keep different chunks (of different sizes) of information separated in a binary file?
Ideally i would like to have different lines for it, but i cannot find a way to do so.










share|improve this question














I'm just starting to learn how to work with python binary files, and i'm trying to write information into a file in binary mode. Since apparently there's no newline command for bytes (like n for strings), what is the easiest way to keep different chunks (of different sizes) of information separated in a binary file?
Ideally i would like to have different lines for it, but i cannot find a way to do so.







python binary






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 15:35









Andres CalvoAndres Calvo

83




83













  • You have to make your own encoding.

    – user202729
    Nov 21 '18 at 15:36











  • When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

    – Patrick Haugh
    Nov 21 '18 at 15:39











  • you mean like defining a custom set of characters to mark a division in my information?

    – Andres Calvo
    Nov 21 '18 at 15:43











  • You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

    – progmatico
    Nov 21 '18 at 16:03













  • With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

    – progmatico
    Nov 21 '18 at 16:07



















  • You have to make your own encoding.

    – user202729
    Nov 21 '18 at 15:36











  • When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

    – Patrick Haugh
    Nov 21 '18 at 15:39











  • you mean like defining a custom set of characters to mark a division in my information?

    – Andres Calvo
    Nov 21 '18 at 15:43











  • You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

    – progmatico
    Nov 21 '18 at 16:03













  • With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

    – progmatico
    Nov 21 '18 at 16:07

















You have to make your own encoding.

– user202729
Nov 21 '18 at 15:36





You have to make your own encoding.

– user202729
Nov 21 '18 at 15:36













When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

– Patrick Haugh
Nov 21 '18 at 15:39





When a text file is encoded, there aren't empty gaps between the lines of text. Instead, a special character n is used to represent line breaks. You'll have to do something similar.

– Patrick Haugh
Nov 21 '18 at 15:39













you mean like defining a custom set of characters to mark a division in my information?

– Andres Calvo
Nov 21 '18 at 15:43





you mean like defining a custom set of characters to mark a division in my information?

– Andres Calvo
Nov 21 '18 at 15:43













You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

– progmatico
Nov 21 '18 at 16:03







You may be interested in grouping your record data in a kind of C struct and then write (or read) the structs (records) from/to a file. See the struct Python module.

– progmatico
Nov 21 '18 at 16:03















With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

– progmatico
Nov 21 '18 at 16:07





With structs of different sizes mixed, you need to know the type before a read or write. See also the pickle format for ideas.

– progmatico
Nov 21 '18 at 16:07












1 Answer
1






active

oldest

votes


















2














What you are referring to is an encoding - the way the bits/bytes in a binary file should be interpreted.



All files are binary files as they are stored. It is only when they are displayed/transmitted/processed that the encoding becomes important.



For example, the bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0A, 0x65, 0x41, 0x42, 0x43 might be displayed as



Hello!
ABC


If there were interpreted as part of a text file by a text editor, because the bytes are ASCII text and 0x0A is a newline character.



However, the same sequence of bytes would be interpreted very differently if they were part of a JPEG file (for example).



As an example of a binary encoding, in a JPEG file each logical piece of image information is called a segment. Each segment starts with a marker. Each marker starts with the byte 0xFF. This is the 'separator' between logical pieces of information.






share|improve this answer























    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',
    autoActivateHeartbeat: false,
    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%2f53415516%2fhow-to-separate-information-in-binary-files-in-python%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    What you are referring to is an encoding - the way the bits/bytes in a binary file should be interpreted.



    All files are binary files as they are stored. It is only when they are displayed/transmitted/processed that the encoding becomes important.



    For example, the bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0A, 0x65, 0x41, 0x42, 0x43 might be displayed as



    Hello!
    ABC


    If there were interpreted as part of a text file by a text editor, because the bytes are ASCII text and 0x0A is a newline character.



    However, the same sequence of bytes would be interpreted very differently if they were part of a JPEG file (for example).



    As an example of a binary encoding, in a JPEG file each logical piece of image information is called a segment. Each segment starts with a marker. Each marker starts with the byte 0xFF. This is the 'separator' between logical pieces of information.






    share|improve this answer




























      2














      What you are referring to is an encoding - the way the bits/bytes in a binary file should be interpreted.



      All files are binary files as they are stored. It is only when they are displayed/transmitted/processed that the encoding becomes important.



      For example, the bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0A, 0x65, 0x41, 0x42, 0x43 might be displayed as



      Hello!
      ABC


      If there were interpreted as part of a text file by a text editor, because the bytes are ASCII text and 0x0A is a newline character.



      However, the same sequence of bytes would be interpreted very differently if they were part of a JPEG file (for example).



      As an example of a binary encoding, in a JPEG file each logical piece of image information is called a segment. Each segment starts with a marker. Each marker starts with the byte 0xFF. This is the 'separator' between logical pieces of information.






      share|improve this answer


























        2












        2








        2







        What you are referring to is an encoding - the way the bits/bytes in a binary file should be interpreted.



        All files are binary files as they are stored. It is only when they are displayed/transmitted/processed that the encoding becomes important.



        For example, the bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0A, 0x65, 0x41, 0x42, 0x43 might be displayed as



        Hello!
        ABC


        If there were interpreted as part of a text file by a text editor, because the bytes are ASCII text and 0x0A is a newline character.



        However, the same sequence of bytes would be interpreted very differently if they were part of a JPEG file (for example).



        As an example of a binary encoding, in a JPEG file each logical piece of image information is called a segment. Each segment starts with a marker. Each marker starts with the byte 0xFF. This is the 'separator' between logical pieces of information.






        share|improve this answer













        What you are referring to is an encoding - the way the bits/bytes in a binary file should be interpreted.



        All files are binary files as they are stored. It is only when they are displayed/transmitted/processed that the encoding becomes important.



        For example, the bytes 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x21, 0x0A, 0x65, 0x41, 0x42, 0x43 might be displayed as



        Hello!
        ABC


        If there were interpreted as part of a text file by a text editor, because the bytes are ASCII text and 0x0A is a newline character.



        However, the same sequence of bytes would be interpreted very differently if they were part of a JPEG file (for example).



        As an example of a binary encoding, in a JPEG file each logical piece of image information is called a segment. Each segment starts with a marker. Each marker starts with the byte 0xFF. This is the 'separator' between logical pieces of information.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 15:57









        jfowkesjfowkes

        777312




        777312






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53415516%2fhow-to-separate-information-in-binary-files-in-python%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

            Create new schema in PostgreSQL using DBeaver

            Deepest pit of an array with Javascript: test on Codility

            Costa Masnaga