gpt4 book ai didi

python - beam.io FileBasedSource 中的 open_file 问题与 python 3

转载 作者:行者123 更新时间:2023-12-05 07:18:06 28 4
gpt4 key购买 nike

我正在使用 CSVRecordSource 读取 Apache Beam 管道中的 CSV,该管道在 read_records 函数中使用 open_file。

使用 python 2 一切正常,但是当我迁移到 python 3 时它会在下面提示

next(csv_reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)

默认情况下,open_file 方法以二进制模式打开文件。

所以我改成使用

with open(filename, "rt") as f:

但是当我在谷歌云中运行数据流时它失败了,因为它找不到文件并给出错误

FileNotFoundError: [Errno 2] No such file or directory

下面是我的代码

 with self.open_file(filename) as f:
csv_reader = csv.reader(f, delimiter=self.delimiter, quotechar=self.quote_character)
header = next(csv_reader)

如何在 python 3 中使用 CSVRecordSource?

最佳答案

您是否使用此处定义的 open_file 方法:https://github.com/apache/beam/blob/6f6feaaeebfc82302ba83c52d087b06a12a5b119/sdks/python/apache_beam/io/filebasedsource.py#L166

如果是这样,我认为您可以调用底层 FileSystems.open() 并将 'application/octet-stream' 替换为 'text/plain '.

关于python - beam.io FileBasedSource 中的 open_file 问题与 python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58500594/

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