-1

I have this data set that I need to align the columns of ID, Date, and Title. I'm not sure how to align these and how to add a header? I'm not able to use any 3rd party functions like tabulate. Could someone walk me through on how to do this? I'm a beginner and just need some guidance!

data_set = [['ID=j234hg\n'], ['Date=19 October 1969\n'], ['Title=court scene with cardinal richelieu\n'], ['ID=d45j5jkd\n'], ['Date=28 December 1969\n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM\n'], ['ID=s4k5jk\n'], ['Date=8 December 1970\n'], ['Title=crossing the atlantic on a tricycle\n'], ['ID=zd7u4h\n'], ['Date=19 October 1969\n'], ['Title=Bicycle Repair Man\n'], ['ID=f983\n'], ['Date=22 December 1970\n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)\n'], ['ID=j8s74\n'], ['Date=15 September 1970\n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTS\n'], ['ID=n4j6l3j\n'], ['Date=7 December 1972\n'], ['Title=Mr. Pither']]

for index,l in enumerate(data_set):
    column_name,value = l[0].split("=")
    if 'Title' == column_name:
        data_set[index]="=".join([column_name+value.title()])

print (data_set)

rows = [ data_set[i:i+3] for i in range(0,len(data_set), 3)]
print (sum(sorted(rows, key = lambda r:r[0][0]), []))

It needs to look like this:

#ID       Date              Title
#d45j5jkd 28 December 1969  The Royal Philharmonic Orchestra Goes To The Bathroom
#f983     22 December 1970  Royal Episode 13 (Or: The Queen Will Be Watching)
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153

1 Answers1

0

You can use the Python3.x Format Specification Mini-Language to format and align the headers and the data_set.

Unfortunately the data_set is a list of sub-lists, so it starts a little messy as we need to Flatten list of lists, then printing in the for-loop uses named placeholders and string padding to print groups of 3 from the flattened list:

data_set = [['ID=j234hg\n'], ['Date=19 October 1969\n'], ['Title=court scene with cardinal richelieu\n'], 
        ['ID=d45j5jkd\n'], ['Date=28 December 1969\n'], ['Title=THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM\n'], 
        ['ID=s4k5jk\n'], ['Date=8 December 1970\n'], ['Title=crossing the atlantic on a tricycle\n'], 
        ['ID=zd7u4h\n'], ['Date=19 October 1969\n'], ['Title=Bicycle Repair Man\n'], ['ID=f983\n'], 
        ['Date=22 December 1970\n'], ['Title=Royal Episode 13 (or: The Queen Will Be Watching)\n'], 
        ['ID=j8s74\n'], ['Date=15 September 1970\n'], ['Title=THE SEMAPHORE VERSION OF WUTHERING HEIGHTS\n'], 
        ['ID=n4j6l3j\n'], ['Date=7 December 1972\n'], ['Title=Mr. Pither']]

#First, flatten the list of lists to one list of strings
flist = [item for sublist in data_set for item in sublist]

print('{h1:10}{h2:20}{h3}'.format(h1='#ID', h2='Date', h3='Title'))
for i in range(0, len(flist), 3):
    print('{id:10}{date:20}{title}'   
                       .format(   
                               id=flist[i].strip('\n').split('=')[1],   
                               date=flist[i+1].strip('\n').split('=')[1],   
                               title=flist[i+2].strip('\n').split('=')[1])
                              )

Output:

#ID       Date                Title
j234hg    19 October 1969     court scene with cardinal richelieu
d45j5jkd  28 December 1969    THE ROYAL PHILHARMONIC ORCHESTRA GOES TO THE BATHROOM
s4k5jk    8 December 1970     crossing the atlantic on a tricycle
zd7u4h    19 October 1969     Bicycle Repair Man
f983      22 December 1970    Royal Episode 13 (or: The Queen Will Be Watching)
j8s74     15 September 1970   THE SEMAPHORE VERSION OF WUTHERING HEIGHTS
n4j6l3j   7 December 1972     Mr. Pither

Feel free to comment if it doesn't exactly answer your question.
I apologize as it is very messy but it works. I'll try to clean it up later. In the meantime, I hope it helps.

Community
  • 1
  • 1
  • When i go through and put this in it doesn't seem to want to take it. I'll have to look at the error again, but it says that there is something going on with the .strip part. – learningpython Mar 10 '17 at 01:16
  • Sorry, yes I didn't expect it would work perfectly the first time you tried it since it's kind of messy. If you're willing, please feel free to post any errors you get and I can try to modify the code to make it work for you. Thanks for your feedback. – chickity china chinese chicken Mar 10 '17 at 01:18
  • Also, please make sure the `data_set` structure is the same as you mentioned in the question, i.e. `[[''ID=j234hg\n''], ['...\n'], ['...\n'], ..., ['Title=Mr. Pither']]` ---- a `list` of `lists` – chickity china chinese chicken Mar 10 '17 at 01:43
  • flist = [data_set] print('{h1:10}{h2:20}{h3}'.format(h1='#ID', h2='Date', h3='Title')) for i in range(0, len(flist), 3): print('{id:10}{date:20}{title}' .format( id=flist[i].strip('\n').split('=')[1], date=flist[i+1].strip('\n').split('=')[1], title=flist[i+2].strip('\n').split('=')[1]) ) – learningpython Mar 10 '17 at 02:43
  • I entered that into my code in the format you had and it gave me: – learningpython Mar 10 '17 at 02:44
  • id=flist[i].strip('\n').split('=')[1], builtins.AttributeError: 'list' object has no attribute 'strip' – learningpython Mar 10 '17 at 02:44
  • Thanks for that info. Sorry, I should have been more clear - you don't want to change `flist`. Keep your `data_set` the same as it is in your code, just copy and paste your `data_set` above my code. Don't change any of the code I posted in the answer, only *add* your `data_set` as the first line. – chickity china chinese chicken Mar 10 '17 at 02:46
  • Works perfectly! Thank you – learningpython Mar 10 '17 at 02:54
  • Wow, great to hear! I'm sure it can be improved, and it's not very clear, but I'm glad it works for you! You're very welcome, glad to help. Cheers! – chickity china chinese chicken Mar 10 '17 at 02:56