I need to write a binary file in python where first few lines are 8 byte ints and the rest a bunch of double precision numbers. How do I best do it? I tried this code, but I got stuck, becuase for some reason, this code generates only text files despite being open in wb.
if binary:
outfile = open(filename,"wb")
text = f"1\n8\n{nrcells}\n{nrspecs}\n"
outfile.write(text.encode(encoding="UTF-8"))
if self.rho.ndim == 3:
for x3 in range(self.par["rho_dim"][2]):
for x2 in range(self.par["rho_dim"][1]):
text="\n".join(f"{d:.8e}" for d in self.rho[:, x2, x3]*10**(-10))+"\n"
outfile.write(text.encode(encoding="UTF-8"))
10, which corresponds to a newline, effectively breaking your file format.