问题 Python - 将文件内容转换为二进制数组
文件内容:
40 13 123
89 123 2223
4 12 0
我需要存储整个 .txt
将文件作为二进制数组存储,以便稍后将其发送到需要二进制输入的服务器端。
我看过Python的 字节组 文档。
我引用:
返回一个新的字节数组。 bytearray类型是0 <= x <256范围内的可变整数序列。它具有可变序列的大多数常用方法,在可变序列类型中描述,以及字节类型具有的大多数方法,请参阅字节和字节数组方法。
我的数字大于256,我需要一个 字节组 数字大于256的数据结构。
8573
2018-03-10 09:05
起源
答案:
你可能会用 array
/memoryview
途径
import array
a = array.array('h', [10, 20, 300]) #assume that the input are short signed integers
memv = memoryview(a)
m = memv.cast('b') #cast to bytes
m.tolist()
然后给出 [10, 0, 20, 0, 44, 1]
根据用途,人们也可以这样做:
L = array.array('h', [10, 20, 300]).tostring()
list(map(ord, list(L)))
这也给了 [10, 0, 20, 0, 44, 1]
7
2018-03-10 09:23
您可以读入文本文件并将每个'word'转换为int:
with open(the_file, 'r') as f:
lines = f.read_lines()
numbers = [int(w) for line in lines for w in line.split()]
然后你必须打包 numbers
用二进制数组 struct
:
binary_representation = struct.pack("{}i".format(len(numbers)), *numbers)
如果您想要写入这些数据 以二进制格式,你必须在打开目标文件时指定:
with open(target_file, 'wb') as f:
f.write(binary_representation)
3
2018-03-10 09:27
不是bytearray
来自 bytearray
文件,它只是0 <= x <256范围内的整数序列。
例如,您可以像这样初始化它:
bytearray([40,13,123,89,123,4,12,0])
# bytearray(b'(\r{Y{\x04\x0c\x00')
由于整数已经以二进制形式存储,因此您无需转换任何内容。
你现在的问题变成了:你想做什么 2223
?
>>> bytearray([2223])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: byte must be in range(0, 256)
uint32或int32数组?
要读取一个文件,您可以使用:
import re
with open('test.txt') as f:
numbers = [int(w) for line in f for w in re.split(' +', line)]
print numbers
#[40, 13, 123, 89, 123, 2223, 4, 12, 0]
一旦有了整数列表,就可以选择相应的低级别 Numpy数据结构,可能 uint32
要么 int32
。
2
2018-03-10 09:19
我需要这个 节约 服务器 - 客户端模块,其功能之一需要一个 binary
输入。可以找到不同的节俭类型 这里。
客户
myList = [5, 999, 430, 0]
binL = array.array('l', myList).tostring()
# call function with binL as parameter
在 服务器 我重建了这个清单
k = list(array.array('l', binL))
print(k)
[5, 999, 430, 0]
1
2017-08-01 07:43
尝试这个:
input.txt中:
40 13 123
89 123 2223
4 12 0
用于解析输入到输出的代码:
with open('input.txt', 'r') as _in:
nums = map(bin, map(int, _in.read().split())) # read in the whole file, split it into a list of strings, then convert to integer, the convert to binary string
with open('output.txt', 'w') as out:
out.writelines(map(lambda b: b + '\n', map(lambda n: n.replace('0b', ''), nums))) # remove the `0b` head from the binstrings, then append `\n` to every string in the list, then write to file
output.txt的:
101000
1101
1111011
1011001
1111011
100010101111
100
1100
0
希望能帮助到你。
0
2018-03-10 09:17