1

I'm using exchangelib and works well but when I'm trying to export item information such as:

data= ("Inbox", item.datetime_received, item.sender, item.subject)

When I use print all items are displayed as expected, each email in new line:

Inbox 2019-10-15, jack, New email information

Inbox 2019-10-16, tom, Hello

Inbox 2019-10-17, anna, Test email

When I'm trying to write this to CSV using code below:

with open("C:/mail_export.csv",'w',newline='\n',encoding="utf-8") as f:
    for item in inbox_folder.all().order_by('-datetime_received'):
        data=("Inbox", item.datetime_received, item.sender, item.subject)
        f.write(str(data))

I got all information in one long line and I can't save this so each item is starting from new line. Now looks like this:

Inbox 2019-10-15, jack, New email information Inbox 2019-10-16, tom, Hello Inbox 2019-10-17, anna, Test email

What I'm doing wrong ? How to write this the same way is displaying when print ? Any help would be very much appreciated!

Community
  • 1
  • 1
Jack84
  • 79
  • 1
  • 1
  • 7

1 Answers1

3

Think you are very close, I'm unable to replicate your issue. But I would suggest you to add \n in the write function because, only then there is a possibility of the code to write it into a new line. Based on your output, All the other steps are executed properly

Manoj Ravi
  • 103
  • 7
  • Thank you for pointing me in the right direction. So small and simple code done the trick. One more time, thank you for your help! – Jack84 Oct 16 '19 at 10:11