简体   繁体   中英

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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