W_A11, 2000-02, Скользящее среднее, 59.66666667, 50.92582302, 68.40751031, Травмы, Число, Нападение, Утверждено, Всего населения, Все возрасты, Смертельно,
W_A11, 2001-03, Скользящее среднее, 60, 51.23477459, 68.76522541, Травмы, Число, Нападение, Утверждено, Всего населения, Все возрасты, Смертельно,
W_A11, 2002-04, Скользящее среднее, 59, 50.30812505, 67.69187495, Травмы, Число, Нападение, Утверждено,Whole pop, All ages, Fatal,
def append_to_datalist(): #Datalist should be called append_to_datafile0,
#change that for the next program
"""Append_to_datalist (should be datafile) first wipes the outfile
clean then appends all read lines containing the
same year specified in querydate() from the infile to the
outfile"""
outfile = open("datalist.csv", "w") #these two lines are for resetting
#the file so it remains manageably
#small
outfile.write('') #this is the second line
outfile = open("datalist.csv", "a")
next(infile)
# extract data
for line in infile:
linefromfile = line.strip('\n').split(',')
tuple1 = tuple(linefromfile)
outfile.write('\n' + str(tuple1))
outfile.close()
def openfile_and_append_to_datalist():
# input for file name
filename = input(
"Please enter the name of the file, including the file extension, from
which we will be extracting data"
" ex)injury_statistics.txt ")
# open infile
infile = open(filename, "r")
# append infile data to outfile
append_to_datalist()
# close infile
infile.close()
openfile_and_append_to_datalist()
Когда я запускаю этот файл, он работает нормально, пока не попытается закрыть infile, а затем вернет «имя ошибки« infile »не определено».
Я не уверен, что попробовать, кроме отмены функции append_to_datalist () из openfile_and_append_to_datalist (), которую я безуспешно пытался.
Причина, по которой мой вопрос говорит, что infile открыт в другой функции, заключается в том, что append_to_datalist () используетвходной_файл.