Python将字节写入文件的顺序不正确问题

2024-07-02 04:46:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Python中得到了以下字节数组,我正在将其写入一个文件。但每一行的显示顺序都是相反的。我把它写错文件了吗?我不能更改Java程序,但我可以更改python脚本解析字节输出的方式

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

这是文件结果

❯ cat filename.txt

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

字节数组是一个java程序的输出,它只读取一个文件,类似这样

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

Tags: 文件程序newdata字节数组lengthstruct
1条回答
网友
1楼 · 发布于 2024-07-02 04:46:59
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)

相关问题 更多 >