Python 如何将.txt文件中的句子添加到数据帧中?

Python 如何将.txt文件中的句子添加到数据帧中?,python,python-3.x,pandas,python-3.6,Python,Python 3.x,Pandas,Python 3.6,我试图读取一个.txt文件,用句子将其分开,并创建一个pandas数据框,其中每行有一个句子。产出将是: 0 "blah blah, blah." 1 "more blah." 2 "more more, blah." 到目前为止,我的代码将.txt文件按句子分开,但我似乎不知道如何获取每个句子并将其附加到数据帧中 import os import sys import pandas as pd import re with open('path/to/file.txt', 'r') as

我试图读取一个.txt文件,用句子将其分开,并创建一个pandas数据框,其中每行有一个句子。产出将是:

0 "blah blah, blah."
1 "more blah."
2 "more more, blah."
到目前为止,我的代码将.txt文件按句子分开,但我似乎不知道如何获取每个句子并将其附加到数据帧中

import os
import sys
import pandas as pd
import re

with open('path/to/file.txt', 'r') as file:
    for line in file:
        for l in re.split(r"(\.)",line):
            string += l
        string += '\n'

假设您有一个循环,该循环将字符串作为句子的列表对象返回,如:

["blah blah, blah.", "more blah.", "more more, blah."]
那么你只需要:

pd.DataFrame(string)
但是你的循环看起来像是在每行的基础上分割句子,而不是跨行。如果需要跨行捕获句子,那么应该这样做:

string = []    
with open("path/to/file.txt", "r") as f:
    full_text = f.read()
    for l in re.split(r"(\.)", full_text):
        if l != ".":
            string.append(l + "\n")
pd.DataFrame(string)

试试pd.read_csv'file.txt'@jp_data_analysis Hmm这不会创建包含单个句子的行,事实上我不确定它是什么格式