Python3如何为" For"声明全局变量环

时间:2017-01-17 18:16:19

标签: python python-3.x global-variables

所以我试图让全局变量在For循环中使用。不在函数中时,此代码可以正常运行" def"但我需要它,因为我试图创建一个tkinter文件对话框,如果你可以帮助告诉我我做错了什么或给我一个替代使用tkinter和" DEF&#34 ;.

工作代码:

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()

编辑代码:

import tkinter
from datetime import datetime
from dateutil.relativedelta import relativedelta
import itertools

#date = datetime.strptime('1991-01-01', '%y-%m-%d')

# 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'
Xref = ''
Yref = ''



def read_date():

    with open(f_path) as file_read:

        for row in itertools.islice(file_read, 5, None):

            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)

2 个答案:

答案 0 :(得分:0)

不推荐使用全局变量的PS仅在最后一种方式使用它们

在你的函数中添加全局关键字,如下所示

my_blobal_var
my_blobal_var2

def my_func():
    global my_blobal_var
    global my_blobal_var2
    do_something()

适合您的情况

Xref = ''
Yref = ''

def read_date():
      global Xref
      global Yref
      do_your_thing()

答案 1 :(得分:0)

不要使用全局变量。这几乎总是一个错误。将参数传递给函数并让它返回结果:

def read_date(path):
    xref = ''
    yref = ''
    with open(path) as file_read:
        for row in itertools.islice(file_read, 5, None):
            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)
    return xref, yref, date, Value

这样,xrefyref,...是read_date函数中的局部变量。完成后,它会将其值返回给调用者。它还将文件路径作为参数,而不是将其绑定到全局变量。你会这样称呼它:

Xref, Yref, date, Value = read_date(f_path)

注意:您的代码示例中不清楚实际有用的数据。只需返回您关注的任何结果,并在调用该函数时以相同的顺序收集它们