You are not logged in.

#1 2014-08-22 11:01:15

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

[SOLVED] need help with formatting in python

HI, i have this little bookmark script in python: https://github.com/carnager/robot/blob/master/robot

there are 2 functions that spit out the same output, but one is filtered (listAll and listURL)
while listAll is formatted nicely, I couldnt manage to format the listURL stuff.

for convinience here are both functions:

def listURL(args):
    bookmarkfile = open(str(bmarks), 'r')
    bookmarks = json.loads(bookmarkfile.read(), 'r')
    bookmarkfile.close()

    if args.group:
        url = [bookmark['name'] + "   " + bookmark['url'] + "   " + ', '.join(bookmark['tags']) + "   " + bookmark['group'] for bookmark in bookmarks if bookmark['group'] == str(args.group)]
        print("\n".join(url))

    if args.tag:
        url = [bookmark['name'] + "   " + bookmark['url'] + "   " + ', '.join(bookmark['tags']) + "   " + bookmark['group'] for bookmark in bookmarks if args.tag in bookmark['tags']]
        print("\n".join(url))

def listAll(args):
    bookmarkfile = open(str(bmarks))
    bookmarks = json.loads(bookmarkfile.read())
    bookmarkfile.close()

    if args.urls:
        for x in bookmarks:
            print("%-40s  %-90s  %77s   %15s" % (x['name'], x['url'], ', '.join(x['tags']), x['group']))

    else:
        for x in bookmarks:
            print("%-40s    %77s   %15s" % (x['name'], ', '.join(x['tags']), x['group']))

Any idea how to format listURL too?

Last edited by Rasi (2014-08-24 00:36:40)


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

#2 2014-08-22 13:49:17

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [SOLVED] need help with formatting in python

I'm a little confused...

Rasi wrote:

i have this little bookmark script in python

What does it do?

there are 2 functions that spit out the same output, but one is filtered (listAll and listURL)
while listAll is formatted nicely, I couldnt manage to format the listURL stuff.

What is the output? What does "filtered" mean? Which one is filtered?

Any idea how to format listURL too?

What do you want to format "listURL" to? JPEG? tongue

Offline

#3 2014-08-23 04:29:06

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: [SOLVED] need help with formatting in python

Use the same format in both functions.

def listURL(args):
    bookmarkfile = open(str(bmarks), 'r')
    bookmarks = json.loads(bookmarkfile.read(), 'r')
    bookmarkfile.close()
    fstr = "%-40s  %-90s  %77s  %15s"

    if args.group:
        for x in bookmarks:
            if x['group'] == str(args.group):
                print(fstr % (x['name'], x['url'], ', '.join(x['tags']), x['group']))

    if args.tag:
        for x in bookmarks:
            if args.tag in x['tags']:
                print(fstr % (x['name'], x['url'], ', '.join(x['tags']), x['group']))

If you want to use Python 3 style formating (which also works with recent versions of python 2), substitute the following:

    fstr = "{:40s}  {:90s}  {:77s}  {:15s}"
    ...
    print(fstr.format(x['name'], x['url'], ', '.join(x['tags']), x['group']))

Offline

#4 2014-08-23 23:34:05

Rasi
Member
From: Germany
Registered: 2007-08-14
Posts: 1,914
Website

Re: [SOLVED] need help with formatting in python

thanks, its working perfectly


He hoped and prayed that there wasn't an afterlife. Then he realized there was a contradiction involved here and merely hoped that there wasn't an afterlife.

Douglas Adams

Offline

Board footer

Powered by FluxBB