Reputation: 660
I'm getting an error when trying to write to a csv file. Here's my two scripts:
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
In tests.py:
with open(get_data().getvalue(), 'rb') as myfile:
data = [row for row in csv.reader(myfile.read().splitlines())]
print data
This is the error:
IOError: [Errno 2] No such file or directory: '999999999\r\n'
Where '999999999' is what should be written in the file.
How can I fix this?
Upvotes: 2
Views: 2083
Reputation: 8774
Your get_data()
function doesn't return a file but a StringIO
object. You don't need to open()
it, you can just read it like this:
myfile = get_data().getvalue()
data = [row for row in csv.reader(myfile.splitlines())]
print data
Upvotes: 2