我正在尝试用python编写一个二进制文件。通常,我使用numpy和struct.pack.
我想复制这个标题取自C#代码:
BinaryWriter writebin = new BinaryWriter(File.Open(..., FileMode.Create))
// write first Line = -99 +chr(13) +chr(10)
writebin.Write((byte)45);
writebin.Write((byte)57);
writebin.Write((byte)57);
writebin.Write((byte)32);
writebin.Write((byte)13);
writebin.Write((byte)10);
C#中的相反读取代码是:
第一步,确定它是否是二进制的:
string[] text = new string[1];
StreamReader reader = new StreamReader(..)
text = reader.ReadLine().Split(new char[] { ' ', ',', 'r', 'n' }, stringSplitOptions.RemoveEmptyEntries);
if (Convert.ToDouble(text[0]) < 0) {
// THEN IT SHOULD GO ON
}
第二步是:
BinaryReader readbin = new BinaryReader(..))
// read 1st line inclusive carriage return and line feed
byte[] header;
header = readbin.ReadBytes(6);
你知道如何在python中做到这一点吗。我就是写不出标题!
谢谢!!!
刚刚找到!我觉得自己很笨。。。
with open(str(filename), 'wb') as writebin:
writebin.write(struct.pack("=B",45))
writebin.write(struct.pack("=B",57))
writebin.write(struct.pack("=B",57))
writebin.write(struct.pack("=B",32))
writebin.write(struct.pack("=B",12))
writebin.write(struct.pack("=B",10))
writebin.write(struct.pack('i', self.nx))
请参阅旧的stackoverflow答案。。。