第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Python 錯(cuò)誤:將 JSON 加載到變量中時(shí),Nonetype 對(duì)象不可下標(biāo)

Python 錯(cuò)誤:將 JSON 加載到變量中時(shí),Nonetype 對(duì)象不可下標(biāo)

縹緲止盈 2021-11-02 09:58:18
我有一個(gè)程序,我正在讀取 JSON 文件,并根據(jù)文件中指定的參數(shù)執(zhí)行一些 SQL。這load_json_file()方法首先將 json 文件加載到 Python 對(duì)象(此處未看到但工作正常)問題在于此處的代碼片段:class TestAutomation:def __init__(self):    self.load_json_file()# connect to Teradata and load session to be used for executiondef connection(self):    con = self.load_json_file()    cfg_dsn = con['config']['dsn']    cfg_usr = con['config']['username']    cfg_pwd = con['config']['password']    udaExec = teradata.UdaExec(appName="DataAnalysis", version="1.0", logConsole=False)    session = udaExec.connect(method="odbc", dsn=cfg_dsn, username=cfg_usr, password=cfg_pwd)    return session該init_方法首先加載JSON文件,然后我存儲(chǔ)在“CON”。雖然我收到一個(gè)錯(cuò)誤,但內(nèi)容如下:cfg_dsn = con['config']['dsn']E   TypeError: 'NoneType' object is not subscriptableJSON 文件如下所示:{    "config":{                                "src":"C:/Dev\\path",                                              "dsn":"XYZ",                                "sheet_name":"test",                                "out_file_prefix":"C:/Dev\\test\\OutputFile_",                                                       "password":"pw123",                                "username":"user123",                                "start_table":"11",                                "end_table":"26",                                "skip_table":"1,13,17",                                "spot_check_table":"77"    }}load_json_file() 定義如下:def load_json_file(self):    if os.path.isfile(os.path.dirname(os.path.realpath(sys.argv[0])) + '\dwconfig.json'):        with open('dwconfig.json') as json_data_file:            cfg_data = json.load(json_data_file)        return cfg_data任何想法為什么我看到錯(cuò)誤?
查看完整描述

2 回答

?
森林海

TA貢獻(xiàn)2011條經(jīng)驗(yàn) 獲得超2個(gè)贊

問題是您正在檢查配置文件是否存在,然后讀取它。


如果沒有,您的函數(shù)將返回None. 這在很多方面都是錯(cuò)誤的,因?yàn)閛s.path.realpath(sys.argv[0])可能返回不正確的值,例如,如果命令僅使用基本名稱運(yùn)行,通過系統(tǒng)路徑找到($0在 bash 中返回完整路徑,但不在 python 或 C 中)。


這不是您獲取當(dāng)前命令目錄的方式。


(加上之后你要做的with open('dwconfig.json') as json_data_file:就是現(xiàn)在的文件名,沒有完整路徑,又錯(cuò)了)


我會(huì)跳過這個(gè)測(cè)試,但正確計(jì)算配置文件路徑。如果它不存在,就讓程序崩潰,而不是返回None稍后會(huì)崩潰的程序。


def load_json_file(self):

    with open(os.path.join(os.path.dirname(__file__),'dwconfig.json')) as json_data_file:

        cfg_data = json.load(json_data_file)

    return cfg_data


查看完整回答
反對(duì) 回復(fù) 2021-11-02
?
翻過高山走不出你

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊

所以... cfg_dsn = con['config']['dsn']

里面的東西被設(shè)置為無

你可以安全地把它寫成這樣

(con or {}).get('config',{}).get('dsn')

或使您的數(shù)據(jù)正確。


查看完整回答
反對(duì) 回復(fù) 2021-11-02
  • 2 回答
  • 0 關(guān)注
  • 397 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)