本文主要是介绍jupyter 外部参数报错 keyerror,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
同样的带有传参的python脚本文件放在在pycharm和cmd 运行没问题,放在jupyter 莫名的报错错误如下:
KeyError Traceback (most recent call last) <ipython-input-2-852e5623add7> in <module>157 if __name__ == '__main__':158 --> 159 main(sys.argv)160 161 <ipython-input-2-852e5623add7> in main(argv)152 #结果文件保存路径153 output_file_str = 'c:/hello/res.xlsx' --> 154 count_working_hours(user_list, rawdata_dir_str, output_file_str)155 156 <ipython-input-2-852e5623add7> in count_working_hours(user_lst, rawdata_dir_str, output_file_str)102 res_df.to_excel(output_file_str)103 else: --> 104 display(res_df[user_lst])105 res_df[user_lst].to_excel(output_file_str)106 E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)2680 if isinstance(key, (Series, np.ndarray, Index, list)):2681 # either boolean or fancy integer index -> 2682 return self._getitem_array(key)2683 elif isinstance(key, DataFrame):2684 return self._getitem_frame(key)E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py in _getitem_array(self, key)2724 return self._take(indexer, axis=0)2725 else: -> 2726 indexer = self.loc._convert_to_indexer(key, axis=1)2727 return self._take(indexer, axis=1)2728 E:\ProgramData\Anaconda3\lib\site-packages\pandas\core\indexing.py in _convert_to_indexer(self, obj, axis, is_setter)1325 if mask.any():1326 raise KeyError('{mask} not in index' -> 1327 .format(mask=objarr[mask]))1328 1329 return com._values_from_object(indexer)KeyError: "['-f'\n 'C:\\\\Users\\\\Administrator\\\\AppData\\\\Roaming\\\\jupyter\\\\runtime\\\\kernel-6bb1336a-63ef-4967-988d-b475a9278451.json'] not in index"
解决问题方法:
将mian函数里参数判断:
if len(argv[1:]):
user_list = argv[1:]
else:
user_list = []
修改为:
if len(argv[1:])==True:
user_list = argv[1:]
else:
user_list = []
这篇关于jupyter 外部参数报错 keyerror的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!