top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Limit Lines of Output using python

0 votes
290 views

I've got a simple script that counts the number of words in a data set (it's more complicated than that, but that's one of the functions), but there are so many words that the output is too much to see in the command prompt window. What I'd like to be able to do is incorporate the "More..." feature that help libraries have, but I have no idea how to do it. I also don't know if I'm asking the question correctly because a Google search yielding nothing.

posted Jun 25, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
I'm running Windows.

2 Answers

0 votes

If you are using linux, you should look up the comand less. It allows you to page thru a test file. You can either write your list to a file or pipe it into less (havent tried that myself)

answer Jun 25, 2013 by anonymous
0 votes

Supposedly, Windows has "more"
[http://superuser.com/questions/426226/less-or-more-in-windows],

For Linux+less; this works:

from subprocess import Popen, PIPE
less = Popen("less", stdin=PIPE)
less.stdin.write(b"n".join("This is line number
{}".format(i).encode("UTF-8") for i in range(1000)))
less.wait()
answer Jun 25, 2013 by anonymous
Similar Questions
0 votes

Does any one have a good solution for how to embed the output of a subprocess (ex. subprocess.Popen("htop", stdout=subprocess.PIPE)) into an ncurses window? So for example, the terminal window is broken up into quadrants and the top right has htop running inside. I'd imagine this would involve some kind of terminal emulation as the dimensions of the window would need to be queried by htop.

0 votes

This question about breaking up really long lines of code in Python.

I have the following line of code:

log.msg("Item wrote to MongoDB database %s/%s" %(settings[MONGODB_DB], settings[MONGODB_COLLECTION]), level=log.DEBUG, spider=spider)

Given the fact that it goes off very far to the right on my screen is not terribly pleasing to my eyes (and can be rude for other developers).

I was thinking of splitting it up like so:

log.msg("Item wrote to MongoDB database %s/%s"
   %(settings[MONGODB_DB], settings[MONGODB_COLLECTION]),
  level=log.DEBUG, spider=spider)

Is this ok? Are there any rules in Python when it comes to breaking up long lines of code?

0 votes

I'm new to Django. Is there any tell how to add a limit filter in Django?

+1 vote

I want to build a class that perform various functions to the content(or lines) of any given file. I want to first include the opening and reading file function into that class, then add other methods.

Here is what I wrote for opening file and reading the lines:

class FileOpration:
 def __init__(self,name):
 self.name = name
 self.filename = self.name
 def readAllline(self):
 open(self.name).readlines()

file1 = FileOpration("myfile.txt")
print file1.filename
allines = file1.readAllline()
print allines

I define self.name variable because I want to store the specific file that I read. This works fine by the test code of print file1.filename. I saw "myfile.txt" on the terminal output.

However, the readAllline() method dose not work. The code only give me "None" as the output on terminal

...