0

I'm developing a CLI app for connecting to WPE's API, meanwhile teaching myself some Python. I hit the API and populate a local sqlite database, and then display the records from that database.

    # pull all our installs
    _ins = self._db.get_all_installs( )

    # fire up and index
    _idx = 1

    # fire up a dict object to hold the selectable item
    _selectable = []

    # loop over the results
    for _row in _ins:

        # gather up some info and through them into re-usable variables
        _wpe_id = _row[2]
        _name = _row[3]
        _environ = _row[4]

        # build a "menu" string
        _menu_str = "{}) {} - {}".format( _idx, _name, _environ )

        # add the items to a dict object
        _selectable.append( _wpe_id )

        # increment the index
        _idx += 1

        # display the item
        if _idx % 3 == 0:

            print("here, maybe??")

        # display the item
        print( _menu_str )

What I am struggling with in figuring out is how I can get this to break up the single-line single-row output. Essentially what I would like to do is see this as a 3 column "menu". How can I do this?

Right now, I have over 300 records, which outputs 1 line at a time. Like:

1) Row 1
2) Row 2
3) Row 3

What I would like to do is like:

1) Row 1       2) Row 2       3) Row 3

etc...

How can I do this?

1 Answer 1

1

You could use end optional argument of print function:

print(_menu_str, end='')

to remove the unwanted \n.

In your code, you would have something like this:

if _idx % 3 == 2:
    print(_menu_str, end='')
else:
    print(_menu_str)
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't know we could do that LOL Pretty close to what I need... oddly though, _idx % 3 ==0 doesnt seem to work right
teach me to post a comment before reloading an edittted answer... that definitely does the trick! thanks a bunch
I changed the condition to _idx % 3 == 2 to get the \n at the right moment :-).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.