0
我的第一行代碼將h設置爲None。然後當我第一次進入for循環時,h會在merge_to_hierarchy方法中得到更新。但是,當我在循環中的第二次時,我的h已經被設置爲None。如何正確更新我的h變量或確保我的第一行代碼只運行一次?變量沒有正確更新,似乎每行代碼都會運行
h = get_hierarchy((script_data.data_version, script_data.elt_type_data[0]))
for fle in glob.glob('./' + script_data.folder_with_csvs + '/*'):
[region, peril, subregion] = (re.sub('.csv', '', (re.sub('./' + script_data.folder_with_csvs + '/', '', fle)))).split("_")
h = merge_to_hierarchy(h, peril, region, subregion)
print(h)
change_file(str(fle))
def change_file(filename):
with open(filename) as f:
r = csv.reader(f)
next(r)
def merge_to_hierarchy(h, peril, region, subregion):
if h is None:
h = Hierarchy(script_data.data_version, script_data.elt_type_data[0], [
PerilRegions(peril, region, Resolutions(script_data.subregion_resolution_data[0], [subregion]),
script_data.subperils, script_data.lobs)])
return h.json()
else:
print("Nothing happened")
也許您需要將其他邏輯應用於您的代碼,但對於代碼,您可以在已設置「h」時設置額外的'variable = value',並在再次運行時檢查此值。 – RST