[英]Python: Store content from a text file as a variable in a dataframe
所以我在幾個地方搜索了我的問題的答案,但是沒有出現。 也許在Python中是不可能的。
我需要將大文本文件的內容存儲為數據框中的一列。 例如:
ID Name Text
1 a 'Full text file 1'
2 b 'Full text file 2'
3 c 'Full text file 3'
4 d 'Full text file 4'
我沒有存儲鏈接或文件名。 我需要像一個很大的字符串一樣存儲內容(文件包含完整的文檔)。 我認為這可能是字典或類似於文本的斑點的東西。
我似乎找不到執行此操作的選項。 有任何想法嗎?
謝謝。
首先使用以下內容創建一個空的數據框:
df = pd.DataFrame(columns=['name', 'text'])
然后,您可以循環讀取文件,並將其放入數據框:
for file in files_list:
my_file = open(file, 'r')
my_text = my_file.read()
my_file.close()
df.loc[len(df)] = [file, my_text]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.