【发布时间】:2016-08-31 13:08:22
【问题描述】:
我有大的 tsv 格式文件(30GB)。我必须将所有这些数据转换为 google bigquery。因此,我将文件拆分成更小的块并 gzip 所有这些块文件并移动到谷歌云存储。之后,我调用了 google bigquery api 从 GCS 加载数据。但我面临以下编码错误。
file_data.part_0022.gz: Error detected while parsing row starting at position: 0. Error: Bad character (ASCII 0) encountered. (error code: invalid)
我在我的 python 代码中使用以下 unix 命令来执行拆分和 gzip 任务。
cmd = [
"split",
"-l",
"300000",
"-d",
"-a",
"4",
"%s%s" % (<my-dir>, file_name),
"%s/%s.part_" % (<my temp dir>, file_prefix)
]
code = subprocess.check_call(cmd)
cmd = 'gzip %s%s/%s.part*' % (<my temp dir>,file_prefix,file_prefix)
logging.info("Running shell command: %s" % cmd)
code = subprocess.Popen(cmd, shell=True)
code.communicate()
文件已成功拆分和压缩(file_data.part_0001.gz、file_data.part_0002.gz 等),但是当我尝试将这些文件加载到 bigquery 时,它会引发上述错误。我知道那是编码问题。 拆分和 gzip 操作时有什么方法可以对文件进行编码?或者我们需要使用python文件对象逐行读取并进行unicode编码并将其写入新的gzip文件?(pythonic方式)
【问题讨论】:
标签: linux python-2.7 unicode google-bigquery google-cloud-storage