How to separate information in binary files in python
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
add a comment |
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
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 charactern
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 ofC struct
and then write (or read) the structs (records) from/to a file. See thestruct
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 thepickle
format for ideas.
– progmatico
Nov 21 '18 at 16:07
add a comment |
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
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
python binary
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 charactern
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 ofC struct
and then write (or read) the structs (records) from/to a file. See thestruct
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 thepickle
format for ideas.
– progmatico
Nov 21 '18 at 16:07
add a comment |
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 charactern
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 ofC struct
and then write (or read) the structs (records) from/to a file. See thestruct
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 thepickle
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
add a comment |
1 Answer
1
active
oldest
votes
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.
add a comment |
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
});
}
});
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%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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 21 '18 at 15:57
jfowkesjfowkes
777312
777312
add a comment |
add a comment |
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.
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%2f53415516%2fhow-to-separate-information-in-binary-files-in-python%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
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 thestruct
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