1

I have a set of text documents (basically they are emails saved as text files.) I have to read these and write in a CSV or Pandas data frame. Each row should take one email or text file. Something like below:

Filename    Content
email_file1 content1
email_file2 content2
email_file3 content3
email_file4 content4
email_file5 content5

I am using this code:

import os
os.chdir('file path')
from pathlib import Path
with open('big.csv', 'w') as out_file:
    csv_out = csv.writer(out_file)
    csv_out.writerow(['FileName', 'Content'])
    for fileName in Path('.').glob('*.txt'):
        csv_out.writerow([str(fileName),open(str(fileName.absolute())).read().strip()])

However, I am getting the following output. The problem with this output is, I am getting a white space (a line gap between each row)

Filename    Content

email_file1 content1

email_file2 content2

email_file3 content3

email_file4 content4

email_file5 content5

I am not sure why this is occurring and how to solve this. Please help.

6
  • strip('\n') to remove newlines. Commented Aug 4, 2017 at 0:42
  • 1
    If you are using python2 or Windows, open your file in wb mode. Commented Aug 4, 2017 at 0:42
  • @cᴏʟᴅsᴘᴇᴇᴅ - I am using python 3. I didnt know that the white space problem i reported as carriage return. I will check the answer you pointed out Commented Aug 4, 2017 at 0:45
  • @cᴏʟᴅsᴘᴇᴇᴅ - Thank you very much for pointing me to the right source. This has worked. If possible, please advise if this question can be closed or deleted since it is a duplicate question as you rightly pointed out. Commented Aug 4, 2017 at 0:51
  • @DoubtDhanabalu A duplicate question serves as a checkpost for future readers to be helpfully directed to the right source. It is perfectly alright to leave it as is. Commented Aug 4, 2017 at 0:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.