Random Binary Read/Write in Python vs .NET

喜夏-厌秋 提交于 2019-12-08 07:08:44

问题


I really like how .NET implements reading/writing of binary data to a file. Clean and elegant.

Can I do this in Python?

Sub Main()
    Using writer As New System.IO.BinaryWriter( _
    System.IO.File.Open("Test.bin", IO.FileMode.Create))
        writer.Write(True)
        writer.Write(123)
        writer.Write(123.456)
        writer.Write(987.654D)
        writer.Write("Test string.")
    End Using
    Using reader As New System.IO.BinaryReader( _
    System.IO.File.Open("Test.bin", IO.FileMode.Open))
        Console.WriteLine(reader.ReadBoolean())
        Console.WriteLine(reader.ReadInt32())
        Console.WriteLine(reader.ReadDouble())
        Console.WriteLine(reader.ReadDecimal())
        Console.WriteLine(reader.ReadString())
    End Using
    Console.Write("Press <RETURN> to exit.")
    Console.ReadKey()
End Sub

来源:https://stackoverflow.com/questions/33201683/random-binary-read-write-in-python-vs-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!