读取csv的文件方法时去除了文件的第一行内容,但是我的表格里都是注册用户的信息
import csvdef file_read(filename): path = '../test_data_1/' + filename with open(path) as file: # file = open('../test_data_1/' + filename) table = csv.reader(file) return tabletable = file_read('regdata.csv')for row in table: print(row)报错信息:for row in table:ValueError: I/O operation on closed file
在学课程Python测试开发全栈核心课程 互联网测试工程师必修课http://www.atstudy.com/course/1287
命令不能混搭呵,你前面用了with,后面就不能用csv.reader
with open(path,"r") as file:
table=file.read()
这样是可以的,要用csv.reader,就用我课上的方法file=open(.......) 使用with是想文件自动关闭。如果用file.read()的话,这样输出就不是按预期的一个cell一个cell输出的,而是一个字符一个字符输出的。我这边上传一下老师的代码
csv.reader()方法返回的数据保存在内存中,关闭文件时,会清空内存。这样就导致table中的数据被清空了,所以要声明一个list作为中间变量,保存csv文件中的值。
import csv
def file_read(filename):
list=[]
path = '../test_data_1/' + filename
with open(path) as file:
# file = open('../test_data_1/' + filename)
table = csv.reader(file)
for row in table:
list.append(row)
return list
table = file_read('regdata.csv')
for row in table:
print(row)
页:
[1]