Python写入字节到文件的顺序错误问题

huangapple go评论79阅读模式
英文:

Python Writing Bytes to File Incorrect Order Issue

问题

以下是代码部分的翻译:

import struct

newFileBytes = [10, 13, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49, 58, 58, 9, 35, 10, 13, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 32, 32, 32, 32, 32, 32, 49, 46, 48, 46, 48, 46, 55, 50, 49, 9, 35, 10, 13, 46, 102, 108, 101, 115, 116, 105, 32, 83, 78, 68, 32, 110, 105, 104, 116, 105, 119, 32, 100, 101, 108, 100, 110, 97, 104, 32, 115, 105, 32, 110, 111, 105, 116, 117, 108, 111, 115, 101, 114, 32, 101, 109, 97, 110, 32, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 35, 10, 13, 10, 13, 116, 115, 111, 104, 32, 116, 110, 101, 105, 108, 99, 32, 120, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 111, 99, 46, 101, 109, 99, 97, 46, 120, 32, 32, 32, 32, 32, 48, 49, 46, 51, 54, 46, 53, 50, 46, 56, 51, 32, 32, 32, 32, 32, 32, 35, 10, 13, 114, 101, 118, 114, 101, 115, 32, 101, 99, 114, 117, 111, 115, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 111, 99, 46, 101, 109, 99, 97, 46, 111, 110, 105, 104, 114, 32, 32, 32, 32, 55, 57, 46, 52, 57, 46, 52, 53, 46, 50, 48, 49, 32, 32, 32, 32, 32, 35, 10, 13, 35, 10, 13, 58, 101, 108, 112, 109, 97, 120, 101, 32, 114, 111, 70, 32, 35, 10, 13, 35, 10, 13, 46, 108, 111, 98, 109, 121, 115, 32, 39, 35, 39, 32, 97, 32, 121, 98, 32, 100, 101, 116, 111, 110, 101, 100, 32, 101, 109, 97, 110, 32, 101, 110, 105, 104, 99, 97, 109, 32, 101, 104, 116, 32, 103, 110, 105, 119, 111, 108, 108, 111, 102, 32, 114, 111, 32, 115, 101, 110, 105, 108, 32, 35, 10, 13, 108, 97, 117, 100, 105, 118, 105, 100, 110, 105, 32, 110, 111, 32, 100, 101, 116, 114, 101, 115, 110, 105, 32, 101, 98, 32, 121, 97, 109, 32, 41, 101, 115, 101, 104, 116, 32, 115, 97, 32, 104, 99, 117, 115, 40, 32, 115, 116, 110, 101, 109, 109, 111, 99, 32, 44, 121, 108, 108, 97, 110, 111, 105, 116, 105, 100, 100, 65, 32, 35, 10, 13, 35, 10, 13, 46, 101, 99, 97, 112, 115, 32, 35, 10, 13, 101, 110, 111, 32, 116, 115, 97, 101, 108, 32, 116, 97, 32, 121, 98, 32, 100, 101, 116, 97, 114, 97, 112, 101, 115, 32, 101, 98, 32, 100, 108, 117, 111, 104, 115, 32, 101, 109, 97, 110, 32, 116, 115, 111, 104, 32, 101, 104, 116, 32, 100, 110, 97, 32, 115, 115, 101, 114, 100, 100, 97, 32, 80, 73, 32, 101, 104, 84, 32, 35, 10, 13, 46, 101, 109, 97, 110, 32, 116, 115, 111, 104, 32, 103, 110, 105, 100, 110, 111

<details>
<summary>英文:</summary>

I&#39;ve got the following byte array in Python that I&#39;m writing to a file. But each line appears in reverse order. Am I writing it to the file wrong? I can&#39;t change the Java program, but I can change the way in which my python script is parsing the byte output. 

import struct

newFileBytes = [10,13,116,115,111,104,108,97,99,111,108,32,32,32,32,32,32,32,32,32,32,32,32,32,49,58,58,9,35,10,13,116,115,111,104,108,97,99,111,108,32,32,32,32,32,32,32,49,46,48,46,48,46,55,50,49,9,35,10,13,46,102,108,101,115,116,105,32,83,78,68,32,110,105,104,116,105,119,32,100,101,108,100,110,97,104,32,115,105,32,110,111,105,116,117,108,111,115,101,114,32,101,109,97,110,32,116,115,111,104,108,97,99,111,108,32,35,10,13,10,13,116,115,111,104,32,116,110,101,105,108,99,32,120,32,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,109,111,99,46,101,109,99,97,46,120,32,32,32,32,32,48,49,46,51,54,46,53,50,46,56,51,32,32,32,32,32,32,32,35,10,13,114,101,118,114,101,115,32,101,99,114,117,111,115,32,35,32,32,32,32,32,32,32,32,32,32,109,111,99,46,101,109,99,97,46,111,110,105,104,114,32,32,32,32,32,55,57,46,52,57,46,52,53,46,50,48,49,32,32,32,32,32,32,35,10,13,35,10,13,58,101,108,112,109,97,120,101,32,114,111,70,32,35,10,13,35,10,13,46,108,111,98,109,121,115,32,39,35,39,32,97,32,121,98,32,100,101,116,111,110,101,100,32,101,109,97,110,32,101,110,105,104,99,97,109,32,101,104,116,32,103,110,105,119,111,108,108,111,102,32,114,111,32,115,101,110,105,108,32,35,10,13,108,97,117,100,105,118,105,100,110,105,32,110,111,32,100,101,116,114,101,115,110,105,32,101,98,32,121,97,109,32,41,101,115,101,104,116,32,115,97,32,104,99,117,115,40,32,115,116,110,101,109,109,111,99,32,44,121,108,108,97,110,111,105,116,105,100,100,65,32,35,10,13,35,10,13,46,101,99,97,112,115,32,35,10,13,101,110,111,32,116,115,97,101,108,32,116,97,32,121,98,32,100,101,116,97,114,97,112,101,115,32,101,98,32,100,108,117,111,104,115,32,101,109,97,110,32,116,115,111,104,32,101,104,116,32,100,110,97,32,115,115,101,114,100,100,97,32,80,73,32,101,104,84,32,35,10,13,46,101,109,97,110,32,116,115,111,104,32,103,110,105,100,110,111,112,115,101,114,114,111,99,32,101,104,116,32,121,98,32,100,101,119,111,108,108,111,102,32,110,109,117,108,111,99,32,116,115,114,105,102,32,101,104,116,32,110,105,32,100,101,99,97,108,112,32,101,98,32,35,10,13,100,108,117,111,104,115,32,115,115,101,114,100,100,97,32,80,73,32,101,104,84,32,46,101,110,105,108,32,108,97,117,100,105,118,105,100,110,105,32,110,97,32,110,111,32,116,112,101,107,32,101,98,32,100,108,117,111,104,115,32,121,114,116,110,101,32,35,10,13,104,99,97,69,32,46,115,101,109,97,110,32,116,115,111,104,32,111,116,32,115,101,115,115,101,114,100,100,97,32,80,73,32,102,111,32,115,103,110,105,112,112,97,109,32,101,104,116,32,115,110,105,97,116,110,111,99,32,101,108,105,102,32,115,105,104,84,32,35,10,13,35,10,13,46,115,119,111,100,110,105,87,32,114,111,102,32,80,73,47,80,67,84,32,116,102,111,115,111,114,99,105,77,32,121,98,32,100,101,115,117,32,101,108,105,102,32,83,84,83,79,72,32,101,108,112,109,97,115,32,97,32,115,105,32,115,105,104,84,32,35,10,13,35,10,13,46,112,114,111,67,32,116,102,111,115,111,114,99,105,77,32,57,48,48,50,45,51,57,57,49,32,41,99,40,32,116,104,103,105,114,121,112,111,67,32,35]
newFile = open("filename.txt", "wb")
newFile.write(struct.pack('<824h', *newFileBytes))


This is the file result.

❯ cat filename.txt

tsohlacol 1:: #
tsohlacol 1.0.0.721 #
.flesti SND nihtiw deldnah si noituloser eman tsohlacol #


The byte array is an output from a java program that simply reads a file, something like this,

byte[] data = null;
File file = new File(filePath);
data = new byte[(int)file.length()];
FileInputStream fis = null;
try {
int rs;
fis = new FileInputStream(file);
for (int i = 0; i < (int)file.length(); i += rs) {
rs = fis.read(data, i, (int)file.length() - i);
}


</details>
# 答案1
**得分**: 1
```python
import itertools
import struct
from more_itertools import split_after
newFileBytes = [10, 13, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 49, 58, 58, 9, 35, 10, 13, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 32, 32, 32, 32, 32, 32, 49, 46, 48, 46, 48, 46, 55, 50, 49, 9, 35, 10, 13, 46, 102, 108, 101, 115, 116, 105, 32, 83, 78, 68, 32, 110, 105, 104, 116, 105, 119, 32, 100, 101, 108, 100, 110, 97, 104, 32, 115, 105, 32, 110, 111, 105, 116, 117, 108, 111, 115, 101, 114, 32, 101, 109, 97, 110, 32, 116, 115, 111, 104, 108, 97, 99, 111, 108, 32, 35, 10, 13, 10, 13, 116, 115, 111, 104, 32, 116, 110, 101, 105, 108, 99, 32, 120, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 111, 99, 46, 101, 109, 99, 97, 46, 120, 32, 32, 32, 32, 32, 48, 49, 46, 51, 54, 46, 53, 50, 46, 56, 51, 32, 32, 32, 32, 32, 32, 35, 10, 13, 114, 101, 118, 114, 101, 115, 32, 101, 99, 114, 117, 111, 115, 32, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 111, 99, 46, 101, 109, 99, 97, 46, 111, 110, 105, 104, 114, 32, 32, 32, 32, 55, 57, 46, 52, 57, 46, 52, 53, 46, 50, 48, 49, 32, 32, 32, 32, 32, 35, 10, 13, 35, 10, 13, 58, 101, 108, 112, 109, 97, 120, 101, 32, 114, 111, 70, 32, 35, 10, 13, 35, 10, 13, 46, 108, 111, 98, 109, 121, 115, 32, 39, 35, 39, 32, 97, 32, 121, 98, 32, 100, 101, 116, 111, 110, 101, 100, 32, 101, 109, 97, 110, 32, 101, 104, 116, 32, 103, 110, 105, 119, 111, 108, 108, 111, 102, 32, 114, 111, 32, 115, 101, 110, 105, 108, 32, 35, 10, 13, 108, 97, 117, 100, 105, 118, 105, 100, 110, 105, 32, 110, 111, 32, 100, 101, 116, 114, 101, 115, 110, 105, 32, 101, 98, 32, 121, 97, 109, 32, 41, 101, 115, 101, 104, 116, 32, 115, 97, 32, 104, 99, 117, 115, 40, 32, 115, 116, 110, 101, 109, 109, 111, 99, 32, 44, 121, 108, 108, 97, 110, 111, 105, 116, 105, 100, 100, 65, 32, 35, 10, 13, 35, 10, 13, 46, 101, 99, 97, 112, 115, 32, 35, 10, 13, 101, 110, 111, 32, 116, 115, 97, 101, 108, 32, 116, 97, 32, 121, 98, 32, 100, 101, 116, 97, 114, 97, 112, 101, 115, 32, 101, 98, 32, 100, 108, 117, 111, 104, 115, 32, 101, 109, 97, 110, 32, 116, 115, 111, 104, 32, 101, 104, 116, 32, 100, 110, 97, 32, 115, 115, 101, 114, 100, 100, 97, 32, 80, 73, 32, 101, 104, 84, 32, 35, 10, 13, 46, 101, 109, 97, 110, 32, 116, 115, 111, 104, 32, 103, 110, 105, 100, 110, 111, 112, 115, 101, 114, 114, 111, 99, 32,
<details>
<summary>英文:</summary>

import itertools
import struct

pip install more-itertools

from more_itertools import split_after

newFileBytes = [10,13,116,115,111,104,108,97,99,111,108,32,32,32,32,32,32,32,32,32,32,32,32,32,49,58,58,9,35,10,13,116,115,111,104,108,97,99,111,108,32,32,32,32,32,32,32,49,46,48,46,48,46,55,50,49,9,35,10,13,46,102,108,101,115,116,105,32,83,78,68,32,110,105,104,116,105,119,32,100,101,108,100,110,97,104,32,115,105,32,110,111,105,116,117,108,111,115,101,114,32,101,109,97,110,32,116,115,111,104,108,97,99,111,108,32,35,10,13,10,13,116,115,111,104,32,116,110,101,105,108,99,32,120,32,35,32,32,32,32,32,32,32,32,32,32,32,32,32,32,109,111,99,46,101,109,99,97,46,120,32,32,32,32,32,48,49,46,51,54,46,53,50,46,56,51,32,32,32,32,32,32,32,35,10,13,114,101,118,114,101,115,32,101,99,114,117,111,115,32,35,32,32,32,32,32,32,32,32,32,32,109,111,99,46,101,109,99,97,46,111,110,105,104,114,32,32,32,32,32,55,57,46,52,57,46,52,53,46,50,48,49,32,32,32,32,32,32,35,10,13,35,10,13,58,101,108,112,109,97,120,101,32,114,111,70,32,35,10,13,35,10,13,46,108,111,98,109,121,115,32,39,35,39,32,97,32,121,98,32,100,101,116,111,110,101,100,32,101,109,97,110,32,101,110,105,104,99,97,109,32,101,104,116,32,103,110,105,119,111,108,108,111,102,32,114,111,32,115,101,110,105,108,32,35,10,13,108,97,117,100,105,118,105,100,110,105,32,110,111,32,100,101,116,114,101,115,110,105,32,101,98,32,121,97,109,32,41,101,115,101,104,116,32,115,97,32,104,99,117,115,40,32,115,116,110,101,109,109,111,99,32,44,121,108,108,97,110,111,105,116,105,100,100,65,32,35,10,13,35,10,13,46,101,99,97,112,115,32,35,10,13,101,110,111,32,116,115,97,101,108,32,116,97,32,121,98,32,100,101,116,97,114,97,112,101,115,32,101,98,32,100,108,117,111,104,115,32,101,109,97,110,32,116,115,111,104,32,101,104,116,32,100,110,97,32,115,115,101,114,100,100,97,32,80,73,32,101,104,84,32,35,10,13,46,101,109,97,110,32,116,115,111,104,32,103,110,105,100,110,111,112,115,101,114,114,111,99,32,101,104,116,32,121,98,32,100,101,119,111,108,108,111,102,32,110,109,117,108,111,99,32,116,115,114,105,102,32,101,104,116,32,110,105,32,100,101,99,97,108,112,32,101,98,32,35,10,13,100,108,117,111,104,115,32,115,115,101,114,100,100,97,32,80,73,32,101,104,84,32,46,101,110,105,108,32,108,97,117,100,105,118,105,100,110,105,32,110,97,32,110,111,32,116,112,101,107,32,101,98,32,100,108,117,111,104,115,32,121,114,116,110,101,32,35,10,13,104,99,97,69,32,46,115,101,109,97,110,32,116,115,111,104,32,111,116,32,115,101,115,115,101,114,100,100,97,32,80,73,32,102,111,32,115,103,110,105,112,112,97,109,32,101,104,116,32,115,110,105,97,116,110,111,99,32,101,108,105,102,32,115,105,104,84,32,35,10,13,35,10,13,46,115,119,111,100,110,105,87,32,114,111,102,32,80,73,47,80,67,84,32,116,102,111,115,111,114,99,105,77,32,121,98,32,100,101,115,117,32,101,108,105,102,32,83,84,83,79,72,32,101,108,112,109,97,115,32,97,32,115,105,32,115,105,104,84,32,35,10,13,35,10,13,46,112,114,111,67,32,116,102,111,115,111,114,99,105,77,32,57,48,48,50,45,51,57,57,49,32,41,99,40,32,116,104,103,105,114,121,112,111,67,32,35]

NEW_LINE_BYTE = 10

Splitting original array, by new-line bytes

corrcet_order_new_file_bytes_arrays = list(split_after(newFileBytes, lambda x: x == NEW_LINE_BYTE))

Reversing each line

corrcet_order_new_file_bytes_arrays = [x[::-1] for x in corrcet_order_new_file_bytes_arrays]

Fusing the list-of-lines to a single list

corrcet_order_new_file_bytes = list(itertools.chain.from_iterable(corrcet_order_new_file_bytes_arrays))

Setting new file bytes with correct, and reversed, lines

newFileBytes = corrcet_order_new_file_bytes

newFile = open("sabich.txt", "wb")
content = struct.pack('<824h', *newFileBytes)
newFile.write(content)


</details>

huangapple
  • 本文由 发表于 2020年8月25日 14:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63573517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定