简体   繁体   English

Python:将文本文件中的内容作为变量存储在数据框中

[英]Python: Store content from a text file as a variable in a dataframe

So I searched several places for an answer to my question, but it's not showing up. 所以我在几个地方搜索了我的问题的答案,但是没有出现。 Maybe it's not possible in Python. 也许在Python中是不可能的。

I need to store the content of large text files as a column in a dataframe. 我需要将大文本文件的内容存储为数据框中的一列。 For example: 例如:

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'

I am not storing links or filenames. 我没有存储链接或文件名。 I need to store the contents like a really large string (the files contain full documents). 我需要像一个很大的字符串一样存储内容(文件包含完整的文档)。 I thought it might be a dictionary or something similar to a blob for text. 我认为这可能是字典或类似于文本的斑点的东西。

I can't seem to find the option to do this. 我似乎找不到执行此操作的选项。 Any ideas? 有任何想法吗?

Thanks. 谢谢。

First create an empty dataframe with: 首先使用以下内容创建一个空的数据框:

df = pd.DataFrame(columns=['name', 'text'])

Then you can read the files in a loop, and put them in the dataframe: 然后,您可以循环读取文件,并将其放入数据框:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM