【发布时间】:2015-11-03 11:38:57
【问题描述】:
尝试写入 csv 文件时出现错误。这是我的两个脚本:
Tasks.py
def get_data():
customer = Customer.objects.all()
csvfile = StringIO.StringIO()
csvwriter = csv.writer(csvfile)
for i in customers:
csvwriter.writerow([str(i.phone_number)])
return csvfile
在tests.py中:
with open(get_data().getvalue(), 'rb') as myfile:
data = [row for row in csv.reader(myfile.read().splitlines())]
print data
这是错误:
IOError: [Errno 2] No such file or directory: '999999999\r\n'
其中 '999999999' 应该写入文件中。
我该如何解决这个问题?
【问题讨论】:
-
你为什么要打开它?它不是一个文件。
get_data是一个可以直接读取的 StringIO 对象。 -
好吧,有道理。谢谢!
标签: python django unit-testing csv testing