Re: help on Implementing a list of dicts with no data pattern
On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: Hi guys, I'm working on this long file, where i have to keep reading and storing different excerpts of text (data) in different variables (list). Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. Please take a look on what i have now: http://pastebin.com/A9eka7p9 Sorry, i thought that a link to pastebin could be helpfully since it captures the syntax highlights and spacings. I don't have a fifty line code there. The 25 lines below, where to show you guys a picture of what is going on, to be more intuitive. This is what i have for now: highway_dict = {} aging_dict = {} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 collumns = ''.join(map(str, content[:1])).replace('-','_').lower() total_values =''.join(map(str, content[1:2])) aging_values = ''.join(map(str, content[2:])) aging_dict['total'], aging_dict[collumns] = total, aging_values queue_row.append(aging_dict) if 'highway' in content: #highway| 4 | disable | 25 collumns = ''.join(map(str, content[:1])).replace('-','_').lower() lanes_values =''.join(map(str, content[1:2])) state_values = ''.join(map(str, content[2:3])).strip('') limit_values = ''.join(map(str, content[3:4])).strip('') highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values queue_row.append(highway_dict) -- http://mail.python.org/mailman/listinfo/python-list
Re: help on Implementing a list of dicts with no data pattern
I apologize once again. Is my first post here and i'm getting used to the group as long as i get the feedback of my errors by you guys. I'm using Python 2.7.3 with no dependencies, i'm simply using the standard library. Here is the big picture of the scenario(i have added it in the pastebin link too) FILE OUTPUT DESIRED: aging: aging |total |age aging |0 |100 aging |2 |115 aging |3 |1 aging |4 |10 highway: highway | lanes | state | limit(mph) highway | 4 | disable | 25 highway | 2 | disable | 245 highway | 3 | disable | 125 highway | 2 | enable | 255 highway | 3 | disable | 212 highway | 8 | disable | 78 FILE INPUT EXCERPT EXAMPLE: aging 0 100 aging 2 115 aging 3 1 highway 4 disable 25 highway 2 disable 245 highway 0 enable 125 Meanwhile i have change the code a little bit and achieve a output closer to what i want: highway_dict = {} aging_dict = {} queue_counters={} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 columns = ', '.join(map(str, content[:1])).replace('-','_').lower() total_values =''.join(map(str, content[1:2])) aging_values = '\t'.join(map(str, content[2:])) aging_dict['total'], aging_dict[columns] = total, aging_values queue_counters[columns] = aging_dict if 'highway' in content: #highway| 4 | disable | 25 columns = ''.join(map(str, content[:1])).replace('-','_').lower() lanes_values =''.join(map(str, content[1:2])) state_values = ''.join(map(str, content[2:3])).strip('') limit_values = ''.join(map(str, content[3:4])).strip('') highway_dict['lanes'], highway_dict['state'], highway_dict['limit(mph)'] = lanes, state, limit_values queue_counters[columns] = highway_dict queue_row.append(queue_counters) Now i'm adding the different dicts to a main one (queue_counters). The problem here is that i'm keeping falling on the iteration issue. I only get the last row on my ouput. My last printout was: queue_counters: {'aging': {'age': '10', 'total': '4'}, 'highway': {'lanes': '8','state': 'disable', 'limit': '78'}} @Dave Angel The sample, or the description, should indicate if repeats of the columns column are allowed, as with b and B above. - Yes the columns repetition are allowed. That line tries to get clever, and ends up obscuring what's really happening. Further, the value in total, if any is NOT what you just extracted in total_values. - this variable name total_values means that i'm storing the content (different values)of the total column, and the same applies to the other columns (might not be the best names, but don't forget that we are just prototyping here). The total, age, etc etc variable name i had to set them, once they don't come with the original file, but is important to give them names to help on RDBMs purposes later, and not only that, it's handy right? The column variable refers to the object name (aging and highway). I'm doing that because in the original source i have to deal with string formatting of strange names. Remember that this is for prototyping, that's why i'm trying to resume things here. -- http://mail.python.org/mailman/listinfo/python-list
Re: help on Implementing a list of dicts with no data pattern
On Thursday, May 9, 2013 12:47:47 AM UTC+1, rlelis wrote: @Dave Angel this is how i mange to read and store the data in file. data = [] # readdata f = open(source_file, 'r') for line in f: header = (line.strip()).lower() # conditions(if/else clauses) on the header content to filter desired data data.append(header) -- http://mail.python.org/mailman/listinfo/python-list
Re: help on Implementing a list of dicts with no data pattern
On Thursday, May 9, 2013 7:19:38 PM UTC+1, Dave Angel wrote: Yes it's a list of string. I don't get the NameError: name 'file_content' is not defined in my code. After i appended the headers i wanted to cut the data list it little bit more because there was some data (imagine some other collumns) to the left that didn't needed. file_content = [] for d in data: file_content.append(d[1:]) from this point on i've showed the code, highway_dict = {} aging_dict = {} queue_counters={} queue_row = [] for content in file_content: if 'aging' in content: # aging 0 100 # code here etc, etc -- http://mail.python.org/mailman/listinfo/python-list
help on Implementing a list of dicts with no data pattern
Hi guys, I'm working on this long file, where i have to keep reading and storing different excerpts of text (data) in different variables (list). Once done that i want to store in dicts the data i got from the lists mentioned before. I want them on a list of dicts for later RDBMs purpose's. The data i'm working with, don't have fixed pattern (see example bellow), so what i'm doing is for each row, i want to store combinations of word/value (Key-value) to keep track of all the data. My problem is that once i'm iterating over the list (original one a.k.a file_content in the link), then i'm nesting several if clause to match the keys i want. Done that i select the keys i want to give them values and lastly i append that dict into a new list. The problem here is that i end up always with the last line repeated several times for each row it found's. Please take a look on what i have now: http://pastebin.com/A9eka7p9 -- http://mail.python.org/mailman/listinfo/python-list