簡體   English   中英

如何將一系列文本文件導入到數據框中,每個文件作為一個輸入,而不是用分隔符分隔?

[英]How can I import a series of text files into a dataframe, with each file as one input, not separated by deliminators?

我想將一系列文本文件導入到數據框中,文件名作為一列,文本字符串在第二列中。

例如,我有兩個文本文件,我希望結果如下所示。

在此處輸入圖片說明

我已經嘗試了下面的代碼塊,但我收到一個錯誤,即 'str' 對象沒有屬性 'name'。 有一個更好的方法嗎? 謝謝!

files = ['TXT1.txt', 'TXT2.txt']
from collections import defaultdict
results = defaultdict(list)
for f in files:
with open(f,"r") as file_open:
    results["file_name"] = file.name
    results["text"].append(file_open.read())
transcriptsDF = pd.DataFrame(results)

你的代碼有錯別字。

file.name應該是file_open.name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM