简体   繁体   English

如何在python中以三进制打开文件?

[英]How to open a file in ternary in python?

I am an amateur python coder. 我是一名业余python编码器。 I was wondering if there was a way to open a file in ternary? 我想知道是否有办法打开三元文件? It would really help to know soon. 很快知道会很有帮助。 Something like this but ternary: 像这样但三元:

with open("C:/Users/jfamily/Documents/NJ_Science_Fair/binary_open_test.txt", 
"rb") as binaryfile :
data = bytearray(binaryfile.read())
print(data)

From the docs about open 来自有关开放的文档

https://docs.python.org/3/library/functions.html#open https://docs.python.org/3/library/functions.html#open

As mentioned in the Overview, Python distinguishes between binary and text I/O. 如概述中所述,Python区分二进制和文本I / O。 Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding. 以二进制模式打开的文件(包括mode参数中的'b')以字节对象的形式返回内容,而无需进行任何解码。 In text mode (the default, or when 't' is included in the mode argument), the contents of the file are returned as str, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given. 在文本模式下(默认模式,或者在模式参数中包含't'时),文件内容以str返回,首先使用依赖于平台的编码或使用给定的指定编码对字节进行了解码。

So really "binary" here means bytes. 因此,这里的“二进制”实际上是指字节。 Those bytes could contain data in some kind of ternary encoding, but you'd likely still open the file as binary and decode them to your ternary spec yourself. 这些字节可能包含某种三元编码的数据,但是您可能仍会以二进制格式打开文件,然后自己将其解码为三元规范。

There is no such facility in Python. Python中没有这样的工具。 The language supports binary because it's a common file format; 该语言支持二进制,因为它是一种常见的文件格式。 ternary is not. 三元不是。

Of course, if you find it useful, I suggest that you write a ternary package of your own. 当然,如果觉得有用,我建议您编写自己的三元程序包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM