如何使用正确的方法将CSV导入BigQuery上的表

时间:2018-02-07 07:39:42

标签: python google-bigquery python-3.6 import-csv

我有一个python脚本执行gbq作业,将csv文件f加载到BigQuery上的表中。所有数据都写入一列但是 我希望它被加载到每一列。我尝试自动检测,但它也没有帮助。 我的csv:

id,first_name,username,last_name,chat_username,chat_id,forward_date,message_text
231125223~Just~koso~swissborg_bounty~-1001368946079~1517903147~test
481895079~Emerson~EmersonEmory~swissborg_bounty~-1001368946079~1517904387~picture
316560356~Ken Sam~ICOnomix~swissborg_bounty~-1001368946079~1517904515~Today 

这是我的代码:

from google.cloud.bigquery import Client
import os
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = '***.json'
os.environ['GOOGLE_CLOUD_DISABLE_GRPC'] = 'True'

from google.cloud import bigquery
dataset_name = 'test_temporary_dataset'
table_name='table_telega'
bigquery_client = bigquery.Client()
dataset = bigquery_client.dataset(dataset_name)
table = dataset.table(table_name)
#table.reload()
job_config = bigquery.LoadJobConfig()
job_config.source_format = 'text/csv'
job_config.skip_leading_rows = 1
job_config.autodetect = True

job_config.allow_jagged_rows=True
job_config.allow_quoted_newlines=True
job_config.fieldDelimiter='~'
with open('tele2.csv', 'rb') as source_file:
    #job = table.upload_from_file(source_file, source_format='text/csv')
    job=bigquery_client.load_table_from_file(source_file, table, job_config=job_config)

job.result()

这就是我得到的: bigquery

如何逐列

正确加载csv

1 个答案:

答案 0 :(得分:5)

自动检测只能用于:

  1. 逗号
  2. 标签
  3. Tilde不适用于自动检测。您需要指定架构,或将分隔符更改为上述之一。

    有关详细信息,请参阅here