![](/img/trans.png)
[英]How I can save each column of Dataframe to separated column in CSV file?
[英]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.