top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration
Print Preview

Limit Lines of Output using python

0 votes
33 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 Google+ Share Button LinkedIn Share Button Multiple Social 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?

+2 votes

How can I flatten just a specific sublist of each list in a list of lists?

So if I had this data

[ ['46295', 'Montauk', '3', '60', '85', ['19', '5', '1', '0 $277790.00']],
 ['46295', 'Dark Eyes', '5', '59', '83', ['6', '4', '1', '0 $105625.00']],
 ['46295', 'Machinegun Jubs', '6', '53', '77', ['6', '2', '1', '1 $71685.00']],
 ['46295', 'Zara Bay', '1', '53', '77', ['12', '2', '3', '3 $112645.00']]]

How can I make it be

[ ['46295', 'Montauk', '3', '60', '85', '19', '5', '1', '0 $277790.00'],
 ['46295', 'Dark Eyes', '5', '59', '83', '6', '4', '1', '0 $105625.00'],
 ['46295', 'Machinegun Jubs', '6', '53', '77', '6', '2', '1', '1 $71685.00'],
 ['46295', 'Zara Bay', '1', '53', '77', '12', '2', '3', '3 $112645.00']]

Been looking around but most solutions just entirely flatten everything. This was popular on SO but yeah it flattens everything I want to be more selective

def flatten(lst):
 for elem in lst:
 if type(elem) in (tuple, list):
 for i in flatten(elem):
 yield i
 else:
 yield elem

What I am thinking is that if for each list the sublist should be at index 1, so

[0][1]
[1][1]
[2][1]

for item in list:
 item[1] - somehow flatten.

Any Idea or pointer?

+1 vote

I want to find the maximal number of elements contained in a nested dictionary, e.g.

data = {
 'violations':
 {
 'col1': {'err': [elem1, elem2, elem3]},
 'col2': {'err': [elem1, elem2]}
 }
 }

so to find the maximal number of elements in the lists for key 'err' in key 'col1' and 'col2'. Also key 'violations' may contain many keys (e.g. 'col1' , 'col2', 'col3' etc), so what's the best way to do this (using a loop)?

max = 0for col in data.violations:
 if max < len(data.violations.col.err):
 max = len(data.violations.col.err)

Useful Links with Similar Problem
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...