Skip to main content
 首页 » 编程设计

python之使用不同大小的部分python加入CSV中的行

2025年05月04日9bonelee

我有一个结构如下的 csv 文件:

|     publish_date     |sentence_number|character_count|    sentence       | 
---------------------------------------------------------------------------- 
|          1           |               |               |                   | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |      -1       |       0       | Sentence 1 here.  | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |       0       |      14       | Sentence 2 here.  | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |       1       |      28       | "Sentence 3 here. | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |       2       |      42       | Sentence 4 here." | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |       3       |      56       | Sentence 5 here.  | 
---------------------------------------------------------------------------- 
|         end          |               |               |                   | 
---------------------------------------------------------------------------- 
|          2           |               |               |                   | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |      -1       |       0       | Sentence 1 here.  | 
---------------------------------------------------------------------------- 
| 02/01/2012  00:12:00 |       0       |      14       | Sentence 2 here.  | 
---------------------------------------------------------------------------- 
|         end          |               |               |                   | 
---------------------------------------------------------------------------- 
|         end          |               |               |                   | 
---------------------------------------------------------------------------- 

我想做的是将每个句子 block 组合成段落以输出单个段落:

["Sentence 1 here.", "Sentence 2 here.", ""Sentence 3 here.", "Sentence 4 here."", "Sentence 5 here."] 

一些句子是引语,可以继续成为一个新句子,而另一些则完全嵌入一个句子中。

到目前为止我得到了这个:

def read_file(): 
 
    file = open('test.csv', "rU") 
    reader = csv.reader(file) 
    included_cols = [3] 
 
    for row in reader: 
        content = list(row[i] for i in included_cols) 
 
        print content     
    return content 
 
read_file() 

但这只是输出一个句子列表,如下所示:

['Sentence 1 here.'] 
['Sentence 2 here.'] 

任何建议表示赞赏。

请您参考如下方法:

只需从每一行中获取第四个元素,您就创建了一个包含每个第四个元素的列表:

def read_file(): 
    file = open('test.csv', "rU") 
    reader = csv.reader(file) 
    return [row[3] for row in reader if len(row) > 3 and row[3]] 

应该输出:

['sentence', 'Sentence 1 here.', 'Sentence 2 here.', ' "Sentence 3 here.', ' Sentence 4 here."', ' Sentence 5 here.', 'Sentence 1 here.', 'Sentence 2 here.'] 

如果你想将段落分成几个部分:

from itertools import groupby 
def read_file(): 
    file = open('temp.txt', "rU") 
    reader = csv.reader(file) 
    paras = (row[3] for row in reader if len(row) > 3) 
    return [list(v) for k, v in groupby(paras,key=lambda x: x != "") if k] 

Groupby 应该输出如下内容:

[['sentence', 'Sentence 1 here.', 'Sentence 2 here.',  
 ' "Sentence 3 here.', ' Sentence 4 here."', ' Sentence 5 here.'], 
 ['Sentence 1 here.', 'Sentence 2 here.']]