gpt4 book ai didi

python - tf.gfile.GFile() 中使用的编码是什么?

转载 作者:行者123 更新时间:2023-12-04 13:45:41 25 4
gpt4 key购买 nike

tf.gfile.GFile() 不接受“编码”参数。来自 here我收集到 gfile 仅返回一个字节流,但现在似乎已更改为:

with tf.gfile.GFile("./data/squad/test1.txt", mode = "rb") as file1:
print(file1.read(n = 2), type(file1.read(n = 2)))
with tf.gfile.GFile("./data/squad/test1.txt", mode = "r") as file1:
print(file1.read(n = 2), type(file1.read(n = 2)))

输出:
b'as' <class 'bytes'>
as <class 'str'>

那么它在读取这些字符串时使用的编码究竟是什么?它是 utf8 还是它依赖于平台,就像 python 中的开放协议(protocol)一样?

最佳答案

据我了解实现,tf.io.gfile.GFile总是使用 UTF-8:https://github.com/tensorflow/tensorflow/blob/b3376f73ccfd6ae8721a946daf064675ee19b427/tensorflow/python/lib/io/file_io.py#L100

def write(self, file_content):
"""Writes file_content to the file. Appends to the end of the file."""
self._prewrite_check()
self._writable_file.append(compat.as_bytes(file_content))
它正在转换 strbytes使用 tf.compat.as_bytes 编码为 UTF-8:

关于python - tf.gfile.GFile() 中使用的编码是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48937652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com