我有一組文本文檔(基本上它們是保存爲文本文件的電子郵件)。我必須讀取這些文檔並以CSV或Pandas數據框編寫。每行應該帶一個電子郵件/文本文件。如何使用Python將一組文本文檔加載到CSV文件中?
我是Python新手。我不知道如何解決這個問題。請幫忙。
Filename Content
email1 Content of email 1
email2 Content of email 2
email3 Content of email 3
… …
… …
… …
email n Content of email 7
編輯
我用下面的代碼
dirpath = 'path'
output = 'output_file.csv'
with open(output, 'w') as outfile:
csvout = csv.writer(outfile)
csvout.writerow(['FileName', 'Content'])
files = os.listdir(dirpath)
for filename in files:
with open(dirpath + '/' + filename) as afile:
csvout.writerow([filename, afile.read()])
afile.close()
outfile.close()
能否請你告訴你已經嘗試了什麼?如果您表明自己已經努力解決您的問題,用戶將更願意爲您提供幫助。 –
@ChristianDean - 謝謝。我更新了我使用的代碼 –