!-----Original Message----- !From: Tutor [mailto:tutor-bounces+crk=godblessthe...@python.org] On !Behalf Of Alan Gauld !Sent: Sunday, October 26, 2014 2:11 AM !To: tutor@python.org !Subject: Re: [Tutor] if you're interested in the code thus far... ! !On 25/10/14 23:46, Clayton Kirkwood wrote: !> __author__ = 'SYSTEM' ! !You are still setting __author__ which is a bit suspect. !Leave double underscores to python.
This is something being created when I started with this file. ! !> import string ! !You are still importing string twice, and you don't use it anywhere that !I can see. ! !> #Pricing Dividends !> !> raw_table = (''' ! !I assume this will eventually come from a file? !You are not really going to store it with your code? !If you are then using a string is pointless and causing you lots of !extra work. ! !> a: Ask y: Dividend Yield !> b: Bid d: Dividend per Share !> b2: Ask (Realtime) r1: Dividend Pay Date !> b3: Bid (Realtime) q: Ex-Dividend Date !... !> s7: Short Ratio !> ''') ! !You don't need parens as well as triple quotes. !The quotes alone are sufficient. Thanks. ! !> import re, string ! !second string import... And you don't seem to be using re either? ! Used them before and then removed the need for them. !> col_position, code, description = 0, [], [] key_name = !> raw_table.replace('\t','\n') ! !I assume this is because you don't control the file format? Since !otherwise you would just use newlines in the file, right? Correct. I cut and pasted the data. Not going to change (probably) no sense in putting it in a file ! !> !> for each_line in key_name.splitlines(): !> if ':' in each_line: !> c, d = each_line.split(':') !> code.append(c) !> description.append(d.strip()) !> print( col_position, code[col_position], !description[col_position]) !> col_position += 1 ! !You could use enumerate in the for loop and that would set the !col_position value for you: ! !for col_position,each_line in enumerate(key_name.splitlines()): Good info, but I tried it and the debugger indicated an IndexError: list.index out of range. Interesting because when I cursor over the various items on the line, they seem fine: col_position was 1 However, I don't think that this solution will work for me, because there are lines with no : these being category type lines such as Date toward the top of the data. But I now have new info on enumerate. ! ! !> output_line_len = 120 !> current_output_pos = index = 0 !> description_output_string = code_output_string = '' !> for description_position, code_position in zip(description, code): ! !Why not just put the codes and descriptions in tuples when you read them !in the loop above? Why use zip? In other words where you do ! ! > c, d = each_line.split(':') ! > code.append(c) ! > description.append(d.strip()) ! !Why not just join the pair there: ! ! > c, d = each_line.split(':') ! values.append((c,d)) ! !or even just ! ! > values.append(each_line.split(':')) ! Uh, because I didn't know about it....:<))) !It seems as if you are splitting the values into two lists only to zip !those lists together again in the next loop? ! !> description_position_len = len(description_position) !> current_output_pos += description_position_len !> !> if current_output_pos >= output_line_len: !> print(description_output_string) !> print(code_output_string) !> code_output_string=description_output_string='' !> current_output_pos=-1 #start new line !> !> description_output_string += '{:^}|'.format(description_position) !> !> code_output_string+='{0:^{1}}|'.format(code_position, !> description_position_len) !> !> current_output_pos+=1 #takes care of '|' at end of string ! This has led to a problem, however. How do I determine if a value is in one of the tuples: Key in [a, word], [key, word] I thought there might be a comprehension or something like a list.key or something. Thanks, Clayton !HTH !-- !Alan G !Author of the Learn to Program web site !http://www.alan-g.me.uk/ !http://www.flickr.com/photos/alangauldphotos ! !_______________________________________________ !Tutor maillist - Tutor@python.org !To unsubscribe or change subscription options: !https://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor