从二进制读取浮点数(每个数字4个字节)到csv文件(Reading from binary as float(4 bytes per number) to csv file)
我想从二进制读取float值并将它们写入一个csv文件,该文件几乎与下面的代码一起使用。 结果是struct.unpack正在写入float值,如下所示:
(数,)
我想写它没有paranteses,()。
有没有更好的方式来获得值作为一个浮动,而不是使用解压缩或你有什么建议?
count = 0
output_file = open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.txt", "w")
with open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.bin", "rb") as f:
while True:
byte = f.read(4)
if not byte:
break
output_file.write(str(unpack('f', byte)))
count = count + 1
if count == 6:
count = 0
output_file.write("\n")
I would like to read from a binary as float values and write them to a csv file, which almost works with the code below. The thing is struct.unpack is writing the float value like:
(number,)
and I would like to write it without the paranteses,().
Is there a better way to get the values as a float instead of using unpack or what do you suggest?
count = 0
output_file = open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.txt", "w")
with open(r"C:\Users\heltbork\Desktop\binTocsvDirect\00000006.bin", "rb") as f:
while True:
byte = f.read(4)
if not byte:
break
output_file.write(str(unpack('f', byte)))
count = count + 1
if count == 6:
count = 0
output_file.write("\n")
原文:https://stackoverflow.com/questions/50468163
更新时间:2020-01-10 18:10
最满意答案
它只是一个tuple的字符串形式。
unpack(...)
给出一个tuple 。这在文档中有说明:
...结果是一个元组,即使它只包含一个项目。
如果你想要第一个元素:
first = unpack('f', byte)[0]
在你的代码中,使用:
output_file.write(str(unpack('f', byte)[0]))
提示:使用csv模块 。
Its just a string form of a tuple.
unpack(...)
Gives back a tuple.This is stated in the docs:
... The result is a tuple even if it contains exactly one item.
If you want the first element:
first = unpack('f', byte)[0]
In your code, use:
output_file.write(str(unpack('f', byte)[0]))
Tip: Use the csv module.
2018-05-22
相关问答
大多数FP工作正常的常见情况,特别是在MS世界中,其中一个文件以文本模式运行,另一个二进制文件运行。 对于C端,确保文件以二进制形式打开 // FILE *istream = fopen(fileanme, "r"); // r
FILE *istream = fopen(fileanme, "rb"); // rb
检查C#侧。 例 int main(void) {
union {
float f;
uint32_t u32;
} u;
u.f =
...
看起来它存储为浮点数,它是32位的。 节点已经可以处理这些常见的数字解析/写入方案。 在这种特殊情况下,您可以使用buf.readFloatBE(0)来转换4个字节。 It looks like it's stored as a float, which is 32-bit. Node can already handle these common number parsing/writing scenarios. In this particular case, you can just use
...
Python本身不支持binary128,因此在标准库中找不到对它们的支持。 您将需要使用NumPy(特别是numpy.frombuffer() )将字节转换为二进制128。 f128 = numpy.frombuffer(file.read(16), dtype=numpy.float128)
Python does not support binary128s natively, hence you won't find support for them in the standard li
...
它只是一个tuple的字符串形式。 unpack(...)
给出一个tuple 。这在文档中有说明: ...结果是一个元组,即使它只包含一个项目。 如果你想要第一个元素: first = unpack('f', byte)[0]
在你的代码中,使用: output_file.write(str(unpack('f', byte)[0]))
提示:使用csv模块 。 Its just a string form of a tuple. unpack(...)
Gives back a tup
...
首先,在你可能正常遇到的任何体系结构中, float s是4个字节,所以当你将4个字节从内存写入一个文件时,没有任何内容被“截断”。 至于你的主要问题,许多常规的文件格式是为了“互操作性”和便于读/写而设计的。 这就是为什么文本是一种几乎普遍可移植的表示(字符编码问题,尽管如此)最常使用。 例如,程序很容易从文本文件中读取字符串“ 123 ”,并知道它代表数字123。 (但是请注意,文本本身并不是一种格式,您可以选择将所有数据元素表示为ASCII / Unicode /任何字符串,并将所有这些字符
...
出于多种原因,二进制文件方法会更好。 你没有提到的一件大事是,你需要将字段中的字符串数据转换为数字数据(至少对于索引,如果不是其他数据),这在两个计算中往往都很昂贵时间和内存。 另外, strtok当然需要搜索字符串,如果你不知道你想要的行直到你看它,那就是很多搜索字符串并将它们转换为你读取的每一行的整数。 The binary file approach will be better for a number of reasons. One of the big pieces that you
...
二进制序列化是类型安全的 。 它确保当你反序列化数据时,你会得到完全相同的对象。 为了做到这一点,BinaryFormatter添加了关于您序列化的对象类型的附加数据。 你会看到额外的开销。 您可以通过序列化到FileStream并使用十六进制查看器查看生成的文件来查看它。 你会看到字符串回来,比如“System.Single”,类型名称和“m_value”,即存储值的字段的名称。 减少开销的一个好方法是,例如,序列化一个数组。 BinaryWriter完全相反,非常紧凑,但不是类型安全的。 两者
...
4字节IEEE格式浮点数约占7位数。 你想要做的是将unpack的结果四舍五入到总共7位数。 从那里,从float到string的正常Python转换将隐藏你的所有浮点肮脏。 def magnitude(x):
return 0 if x==0 else int(math.floor(math.log10(abs(x)))) + 1
def round_total_digits(x, digits=7):
return round(x, digits - magnitude(x)
...
你有一个字节顺序不匹配。 DataOutputStream以big-endian写入,但Float32Array通常以little-endian(取决于硬件)读取。 您必须更改Java端或Javascript端才能匹配。 You've got a byte order mismatch. DataOutputStream writes in big-endian, but Float32Array usually reads in little-endian (depending on hardw
...
以下代码将使用big endian从buffer创建一个unsigned short : unsigned short us = (buffer[5] << 8) | buffer[6];
The following code will create an unsigned short from buffer in big endian: unsigned short us = (buffer[5] << 8) | buffer[6];