小编Joh*_*dun的帖子

Python:如何捕获输出到文本文件?(现在只捕获530条线路中的25条)

我已经做了相当多的潜伏在SO和相当多的搜索和阅读,但我必须承认在编程一般是一个相对的菜鸟.我正在努力学习,所以我一直在玩Python的NLTK.在下面的脚本中,我可以让一切工作,除了它只写出多屏幕输出的第一个屏幕,至少我正在考虑它.

这是脚本:

#! /usr/bin/env python

import nltk

# First we have to open and read the file:

thefile = open('all_no_id.txt')
raw = thefile.read()

# Second we have to process it with nltk functions to do what we want

tokens = nltk.wordpunct_tokenize(raw)
text = nltk.Text(tokens)

# Now we can actually do stuff with it:

concord = text.concordance("cultural")

# Now to save this to a file

fileconcord = open('ccord-cultural.txt', 'w')
fileconcord.writelines(concord)
fileconcord.close()
Run Code Online (Sandbox Code Playgroud)

这是输出文件的开头:

Building index...
Displaying 25 of 530 matches:
y . …
Run Code Online (Sandbox Code Playgroud)

python nltk python-2.7

4
推荐指数
1
解决办法
2335
查看次数

标签 统计

nltk ×1

python ×1

python-2.7 ×1