Skip to content Skip to sidebar Skip to footer

Python: How To Capture Output To A Text File? (only 25 Of 530 Lines Captured Now)

I've done a fair amount of lurking on SO and a fair amount of searching and reading, but I must also confess to being a relative noob at programming in general. I am trying to lear

Solution 1:

text.concordance(self, word, width=79, lines=25) seem to have other parameters as per manual.

I see no way to extract the size of concordance index, however, the concordance printing code seem to have this part: lines = min(lines, len(offsets)), therefore you can simply pass sys.maxint as a last argument:

concord = text.concordance("cultural", 75, sys.maxint)

Added:

Looking at you original code now, I can't see a way it could work before. text.concordance does not return anything, but outputs everything to stdout using print. Therefore, the easy option would be redirection stdout to you file, like this:

import sys

....

# Open the file
fileconcord = open('ccord-cultural.txt', 'w')
# Save old stdout stream
tmpout = sys.stdout
# Redirect all "print" calls to that file
sys.stdout = fileconcord
# Init the method
text.concordance("cultural", 200, sys.maxint)
# Close file
fileconcord.close()
# Reset stdout in case you need something else to print
sys.stdout = tmpout

Another option would be to use the respective classes directly and omit the Text wrapper. Just copy bits from here and combine them with bits from here and you are done.

Solution 2:

Update:

I found this write text.concordance output to a file Options from the ntlk usergroup. It's from 2010, and states:

Documentation for the Text class says: "is intended to support initial exploration of texts (via the interactive console). ... If you wish to write a program which makes use of these analyses, then you should bypass the Text class, and use the appropriate analysis function or class directly instead."

If nothing has changed in the package since then, this may be the source of your problem.

--- previously ---

I don't see a problem with writing to the file using writelines():

file.writelines(sequence)

Write a sequence of strings to the file. The sequence can be any iterable object producing strings, typically a list of strings. There is no return value. (The name is intended to match readlines(); writelines() does not add line separators.)

Note the italicized part, did you examine the output file in different editors? Perhaps the data is there, but not being rendered correctly due to missing end of line seperators?

Are you sure this part is generating the data you want to output?

concord = text.concordance("cultural")

I'm not familiar with nltk, so I'm just asking as part of eliminating possible sources for the problem.

Post a Comment for "Python: How To Capture Output To A Text File? (only 25 Of 530 Lines Captured Now)"