将变量分配给有效负载中的JSON元素

时间:2017-01-29 16:47:40

标签: arrays json list file python-3.x

如何使用data1的变量赋值修复语法错误? 'data1'变量打印正确但我在json.dumps之前将变量放入JSON格式到'data'变量时出现语法错误:

def queryFileID():

    with open('Payload.txt', 'r') as dd:
        payload = []
        for line in dd:
            payload.append(line)

    data1 = payload[0]

    data = json.dumps({
        "filters":{
        "op":"in",
        "content":{
            "field":"files.file_id",
                "value": data1 ,
                "format":"TSV",
                "fields":"case_id,",
                "size":"1000000",
    })

    with open('CaseIDs.txt', 'wb') as gdc:

        c = pycurl.Curl()
        c.setopt(pycurl.URL, GDC_URL_legacy)
        c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.POSTFIELDS, data)
        c.setopt(pycurl.WRITEDATA, gdc)
        c.perform()
        c.close()

有效的代码:

import os
import pycurl
import json

GDC_URL_legacy = 'https://gdc-api.nci.nih.gov/legacy/files/'

data = json.dumps({
    "filters":{
        "op":"in",
        "content":{
            "field":"files.file_id",
            "value":
       ["6998b65f-568a-4636-a6cb-07fb23e984a7",]}},
    "format":"TSV",

"fields":"cases.case_id",
    "size":"1000000"
})

with open('CasedIDs.txt', 'wb') as tgc:

    c = pycurl.Curl()
    c.setopt(pycurl.URL, GDC_URL_legacy)
    c.setopt(pycurl.HTTPHEADER, ['Content-Type: application/json'])
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, data)
    c.setopt(pycurl.WRITEDATA, tgc)
    c.perform()
    c.close()

0 个答案:

没有答案