所以我尝试在For循环中使用全局变量。当不在函数“def”中时,此代码可以正常工作,但我需要它,因为我正在尝试创建一个tkinter文件对话框,该对话框将在选择文件后运行该代码,如果您可以帮助告诉我我做错了什么,或者给我一个使用tkinter和“def”的替代方法。在
工作代码:import os
import sqlite3
from datetime import datetime
# import datetime
import itertools
from dateutil.relativedelta import relativedelta
date = datetime.strptime('1991-01-01', '%Y-%m-%d')
f_path = 'C:/Users/Lewis Collins/Job_Code/data/cru_ts_2_10.1991-2000_cutdown.pre'
# db = sqlite3.connect('output.db')
# cursor = db.cursor()
# cursor.execute('CREATE TABLE Rainfall (Xref, Yref, Date, Value)')
# date = datetime.date(1991,1,1)
flag = 0
Xref = ''
Yref = ''
with open(f_path) as file_read:
for row in itertools.islice(file_read, 5, None):
# print(row)
if 'Grid-ref' in row:
Xref = row.split(',')[0].split('= ')[1]
Yref = row.split(',')[1]
date = datetime.strptime('1990-12-01', '%Y-%m-%d')
else:
for Value in row.split():
date = date + relativedelta(months=+1)
# print(Xref.strip(), Yref.strip(), date, Value)
# print(Xref, Yref, date, Value)
# db.commit()
# db.close()
编辑代码:
^{pr2}$