简体   繁体   中英

Python exchangelib - exporting items from Inbox folder to csv

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM