我一直在撰寫代碼來幫助自動化我作業的實驗室中的一個流程。為了總結代碼的全部目的,我正在創建一種獲取實驗資料、創建資料檔案并將其發送到保留所有這些資訊的網站主機的方法。我在弄清楚如何創建一個字典來填寫從某個開始和結束日期開始的樣本 ID 時遇到了一些麻煩。這是我遇到問題的代碼部分:
def get_vcf(run_name, start_date, end_date, auth_token):
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': auth_token,
}
params = {
'format': 'json',
'name': run_name,
'start_date': start_date,
'end_date': end_date,
}
response = requests.get('https://ionreporter.thermofisher.com/api/v1/getvcf', params=params, headers=headers,
verify=False)
# todo this will return everything in the date range, we will want a dictionary for each of the the sampleIDs
# todo paired with the VCFs it goes with that we will send to XXX, or some csv like list of the samples-
vcfs = response.json()
# todo dictionary construction
vcf_dict = {0: vcfs[0]}
return vcfs, vcf_dict
來自 Web 查詢結果的示例記錄如下所示:
[
{
"data_links": "ionreporter.thermofisher.com/api/v1/download? filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample /Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip",
"name": "Sample_c150_2016-04-29-14-16-534",
"id": "ff808181545d90790154613336be0008"
}
]
在底部,我開始創建字典。我知道這只會回傳來自 vcfs 的第一個回應。我希望它填充我們在日期范圍內擁有的任何數量的樣本資訊,因為它會有所不同。提前致謝!
uj5u.com熱心網友回復:
您提供的代碼表明您需要一個帶有從 . 開頭的數字鍵的字典0。如果是這樣,以下是獲取此類字典的方法,其中值直接取自 Web 查詢的結果:
r = {i: v for i, v in enumerate(vcfs)}
pprint(r)
三項的結果如下所示:
{0: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0008',
'name': 'Sample_c150_2016-04-29-14-16-534'},
1: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0009',
'name': 'Sample_c150_2016-04-29-14-16-534'},
2: {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0010',
'name': 'Sample_c150_2016-04-29-14-16-534'}}
這不是很有用,因為您已經可以通過原始記錄串列中的相同索引查找值。相反,您希望id每條記錄的欄位成為字典中的鍵。然后你會這樣做:
r = {entry['id']: entry for entry in vcfs}
pprint(r)
這將給出這樣的結果:
{'ff808181545d90790154613336be0008': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0008',
'name': 'Sample_c150_2016-04-29-14-16-534'},
'ff808181545d90790154613336be0009': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0009',
'name': 'Sample_c150_2016-04-29-14-16-534'},
'ff808181545d90790154613336be0010': {'data_links': 'ionreporter.thermofisher.com/api/v1/download? '
'filePath=/data/IR/data/IR_Org/[email protected]/JohnSmithSample '
'/Sample_20160429014705727/Sample_c150_2016-04-29-14-16-534.zip',
'id': 'ff808181545d90790154613336be0010',
'name': 'Sample_c150_2016-04-29-14-16-534'}}
為了獲取這些資料,我獲取了您提供的資料記錄,另外制作了兩個副本,并稍微更改了id每個副本的 id 以提供唯一的 ID。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/528998.html
上一篇:更新字典字典
