将句子分成新的一行

时间:2019-01-01 09:30:02

标签: python regex

我有一个采用以下格式的数据集:

DataTable

它们之间用制表符分隔,并且彼此之间并不独立,这意味着每一行中都有许多句子,每个句子都提供电影评论。

我的目标是将每个句子分成带有该标签的新行(1或0,表示否定/肯定评论)。我使用了这样的正则表达式:

using (var conn = new OracleConnection(connectionString))
using (var cmd = new OracleCommand("ProcedureName", conn) { 
                       CommandType = CommandType.StoredProcedure }) {
conn.Open();
using(OracleDataAdapter da = new OracleDataAdapter (cmd))
 {
   DataTable dataTable = new DataTable();
   da.Fill(dataTable);
   dataGridView1.DataSource = dataTable;
 }
 conn.Close();

但是,结果是它仅显示每个句子的标签,而不显示我要查找的内容。我正在寻找的是这样的:

The Da Vinci Code book is just awesome.1      this was the first clive cussler i've ever read, but even books like Relic, and Da Vinci code were more plausible than this.1      i liked the Da Vinci Code a lot.1     da vinci code was an awesome movie...1      the last stand and Mission Impossible 3 both were awesome movies.1     mission impossible 2 rocks!!....1     I love Harry Potter, but right now I hate it ( me younger sis's watching it ).1

或者,有任何适合分类的可能方式,以及与大熊猫一起工作?

我如何实现我的目标?

3 个答案:

答案 0 :(得分:0)

您可以使用

(?<=\.)([0-1])\s*
  • (?<=\.)-正向后方检查.
  • ([01])-捕获组匹配01
  • \s*-匹配零个或多个空格。

Demo

答案 1 :(得分:0)

您可以执行以下操作:

import re
text_file = open('training.txt', 'r')
str_file = text_file.readlines()
p = re.compile("[ \t]{2,}")     # regex for 2 or more spaces
s = p.split(str_file[0])

print(s) 

更新后的代码(由于未知readlines()的实际内容/格式,因此使用training.txt

import re
text_file = open('training.txt', 'r')
str_file = text_file.readlines()
p = re.compile("[ \t]{2,}")     # regex for 2 or more spaces
s = p.split(str_file[0])
print(s)           

它会像这样产生list的{​​{1}}:

strings

答案 2 :(得分:0)

更新(带有Python逻辑的代码)     删除我创建的其他列表;这只是解决方法。

text_file = open('training.txt', 'r')  
file = text_file.readlines()  
s = []  
a = []  
b = []  

import re  

for line in file:  
    a = re.match(".*?[^\s][?=(1|0)]",line)  
    if a == None:  
        pass  
    else:  
        b = a.group()    
        s.append(b)  
print (s)  

我在文件中使用的数据如下。它只会获取以1或0结尾的评论,并将这些句子添加到列表中。

虚拟数据
测试数据
测试数据错误
将添加一些正确的数据进行测试
达芬奇密码书真棒。1
这是我有史以来第一个手抄写的人,但即使像Relic和达芬奇密码这样的书,也比这更可信。1
我非常喜欢达芬奇密码。1达芬奇密码是一部很棒的电影... 1
最后一站和《碟中谍3》都是很棒的电影。1
任务不可能2块石头!! ... 1
我爱哈利·波特(Harry Potter),但现在我讨厌它(我小妹妹正在看它)。1

结果 This is how list looks, list S

相关问题