python - 如何按句子编号、句子(按 '|' 分割)在 CSV 中写入文件?

标签 python csv file-io

所以我试图读取文件列表,提取文件 ID 和摘要。摘要的每个句子都应写入 CSV 文件,其中包含文件 ID、句子编号和句子,并以“|”分隔。

有人告诉我使用 NLTK 的分词器。我安装了 NLTK,但不知道如何让它与我的代码一起工作。我的Python是3.2.2。下面是我的代码:

import re, os, sys
import csv
# Read into the list of files.
topdir = r'E:\Grad\LIS\LIS590 Text mining\Part1\Part1' # Topdir has to be an object rather than a string, which means that there is no paranthesis.
matches = []
for root, dirnames, filenames in os.walk(topdir):
    for filename in filenames:
        if filename.endswith(('.txt','.pdf')):
            matches.append(os.path.join(root, filename))

# Create a list and fill in the list with the abstracts. Every abstract is a string in the list.
capturedabstracts = []
for filepath in matches[:10]:  # Testing with the first 10 files.
    with open (filepath,'rt') as mytext:
    mytext=mytext.read()

        # code to capture files
    matchFile=re.findall(r'File\s+\:\s+(\w\d{7})',mytext)[0]
    capturedfiles.append(matchFile)


    # code to capture abstracts
    matchAbs=re.findall(r'Abstract\s+\:\s+(\w.+)'+'\n',mytext)[0]
    capturedabstracts.append(matchAbs)
    print (capturedabstracts)

with open('Abstract.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for data in capturedabstracts:
    writer.writerow([data])

我是Python的初学者,我可能无法理解你的评论,如果你能提供修改后的代码的评论,那就太好了。

最佳答案

作为第一次尝试,请查看 a sentence tokenizer并将文本拆分为列表,然后使用 writerows 存储到 csv:

with file(u'Abstract.csv','w') as outfile:
    sent_detector = nltk.data.load('tokenizers/punkt/english.pickle')
    list_of_sentences = sent_detector.tokenize(text.strip())
    writer = csv.DictWriter(outfile, headers = ['phrase'], delimiter = '|',  quotechar = None, quoting = csv.QUOTE_NONE, escapechar="\\")
    for phrase in list_of_sentences:
        phrasedict = {'phrase':phrase}
        writer.writerow(phrase)
    writer.close()

关于python - 如何按句子编号、句子(按 '|' 分割)在 CSV 中写入文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22210026/

相关文章:

python -> "operator"用于定义函数

ruby - 解析包含标题字段作为每行属性的 CSV 文件

csv - 从 csv 创建 GeoJson : How do I set geometry properties?

java - 如何获取jar文件内的文件夹路径?

python - llvmlite 中的链接 C

python - OpenCV:OCR 之前阴影图像的轮廓检测

python - 从记录 ndarray 中选择行范围

java - 逐列写入 CSV 文件

perl - 在Perl中花哨的文件

ruby - 如何使用 Ruby 从文件加载数组?