2017-07-17 107 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

謝謝你解決了我的問題! –