如何按句子编号和句子(用'|'分隔)写入CSV文件?
我正在尝试读取一系列文件,从中提取文件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的初学者,可能无法理解你们的评论。如果你能提供带有修改代码的注释,那就太好了。
2 个回答
1
作为一个初步的尝试,可以看看一个句子分割工具,把文本分割成一个列表,然后用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()
0
试试用 writerow
这个方法。
可以尝试这样做:
with open('Abstract.csv', 'w') as csvfile:
writer = csv.writer(csvfile)
for data in capturedabstracts:
writer.writerow([data])