2017-07-17 118 views
0

我试图使用Python脚本从Google云端存储以JSON格式导出表格。当我手动将表格作为来自BigQuery的JSON导出时,它以这种格式完成。导出的JSON格式不正确

{"f0_":5586.2928892104655} 

但是,当我使用我的Python脚本下载它时,我以这种格式接收它。

f0_ 
5586.2928892104655 

这是我一直用来导出和下载JSON的代码。

def export_data_to_gcs(data, test2, destination): 
    bigquery_client = bigquery.Client(data) 
    dataset = bigquery_client.dataset('FirebaseArchive') 
    table = dataset.table('SumConnectionTime') 
    job_name = str(uuid.uuid4()) 

    job = bigquery_client.extract_table_to_storage(
     job_name, table, 'gs://firebase_results/SumConnectionTime.json') 
    job.source_format = 'NEWLINE_DELIMITED_JSON' 

    job.begin() 

    wait_for_job(job) 



def wait_for_job(job): 
    while True: 
     job.reload() 
     if job.state == 'DONE': 
      if job.error_result: 
       raise RuntimeError(job.errors) 
      return 
     time.sleep(1) 

export_data_to_gcs(data, 'SumConnectionTime', destination) 

client = storage.Client(project=data) 
bucket = client.get_bucket('firebase_results') 
blob = bucket.blob('SumConnectionTime.json') 
with open('SumConnectionTime.json', 'w') as file_obj: 
    blob.download_to_file(file_obj) 

我需要它是我最初收到的格式,因为我运行一个json.load与给定的值。谢谢您的帮助。

+0

看来,我们需要在你的'blob'对象的'download_to_file'方法来看一看,因为这是该文件被写入。此外,用于读取“桶”的“blob”方法。 –

+0

会不会有另一种方式来下载文件,而不使用'blob'方法? –

+0

我怎么知道我是否不知道该方法在做什么?它似乎是一种自定义的方法,没有什么来自标准库。 –

回答

1

我怀疑你的问题是你没有指定你希望BigQuery导出到的目标格式。如果你想JSON,尝试与此更换有关source_format您行:

job.destination_format = NEWLINE_DELIMITED_JSON 
+0

谢谢你解决了我的问题! –