如何使用Python脚本

时间:2017-08-14 06:52:15

标签: python google-bigquery

我需要将平面文件中的记录上传到BigQuery表。 以下是使用分隔符上传的示例文件' $':

LCR_REF_OPS_DEPOSITS$amm_reporting_row:string,amm_section:string,amm_report_row_desc:string,amm_subsection_row:float,amm_subsection_desc:string$$csv$|$REF$1$"$$$$TRUE$Sample file$ALT REF

问题是,在其中一个列中我需要上传引号字符(")并且因为它而导致上传失败。 以下是我正在使用的API的代码片段:

bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset('DATASET1')
table = dataset.table('SAMPLE_TAB')
# Reload the table to get the schema. 
table.reload()
with open('testfile.txt', 'rb') as source_file:
    # This example uses CSV, but you can use other formats. 
    # See https://cloud.google.com/bigquery/loading-data 
    job = table.upload_from_file(
        source_file, source_format='text/csv', field_delimiter='$') 

遇到错误:

google.cloud.exceptions.BadRequest: 400 CSV table encountered too many errors, giving up. Rows: 1; errors: 1.

有人可以提出方法吗?

1 个答案:

答案 0 :(得分:1)

BigQuery的"字符为used as default,用于在读取数据时包含记录。

这可能是您的解决方法:

with open('testfile.txt', 'rb') as source_file:
    job = table.upload_from_file(
        source_file,
        source_format='text/csv',
        field_delimiter='$',
        quote_character='')