这是我代码的一部分,我试着运行它并在第12行收到这个错误此处:ValueError:对关闭的文件执行I/O操作。但我确信文件“currentRecords”是打开的。怎么了?你知道吗
c.execute("SELECT * FROM Student, Behaviour")
data = c.fetchall()
currentRecords = open('Current Records - Unsorted', 'w')
l = []
for i in data: #for individual records in the whole database do:
record = str(i)
record = record.replace("u'","")
record = record.replace("'", "")
record = record.replace("(","")
record = record.replace(")", "")
record = record.replace(","," -")
currentRecords.write(record+"\r\n")
currentRecords.write('----------------------------------'+"\r\n")
currentRecords.close()
y = open('Current Records - Unsorted','r')
z = y.read() #opening the file containing the unsorted, formatted records to read
l.append(z)
y.close() #z is an array that holds all the records (each record has its own index within l)
您似乎想写出用破折号分隔的字段值。我们来看看正确的方法。你知道吗
您需要的代码是:
这会将每个字段转换为一个字符串(如果它还不是一个),并用破折号连接字符串,然后在行尾之间插入破折号。你知道吗
Jambofun解释了原因。下面是一个更有效的方法:
注意最后一行,我不确定你在做你想做的事。你现在正在积累所有的记录。你知道吗
相关问题 更多 >
编程相关推荐