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.

Doubt Dhanabalu
  • 457
  • 4
  • 8
  • 18
  • `strip('\n')` to remove newlines. – Luke Aug 04 '17 at 00:42
  • 1
    If you are using python2 or Windows, open your file in `wb` mode. – cs95 Aug 04 '17 at 00: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 – Doubt Dhanabalu Aug 04 '17 at 00: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. – Doubt Dhanabalu Aug 04 '17 at 00: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. – cs95 Aug 04 '17 at 00:52
  • @cᴏʟᴅsᴘᴇᴇᴅ - Thank you very much. I am new to both python as well as Stackoverflow. I will learn how to search effectively. Thanks again for your advise and suggestion. – Doubt Dhanabalu Aug 04 '17 at 01:04

0 Answers0