2 Replies - 412 Views - Last Post: 21 March 2013 - 07:32 AM Rate Topic: -----

#1 donfanzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-April 12

Python file to list to dictionary

Posted 21 March 2013 - 03:53 AM

Hi guys, I'm trying to convert a file (not CSV or anything just flat file) into key value pairs (dictionary) one large dictionary in the format:

detail_data = {{'a': '51', 'b': 'P ', 'c': '444', 'd': '88888888 ', 'e': 'G'},{'a': '89', 'b': 'U ', 'c': '333', 'd': '7777777', 'e': 'O'}} etc...

In order to do this I first need to insert my file into a list so I can use an index for accessing each individual lines as the first and last lines are different to the overall data in my file. Once I have my list I will append the data onto my dict keys on a character by character basis.

my_file = open('FOO.TXT', 'r')
detail_data = {}

def build_data_point(line_index, index_list):
    data_point = ''
    for i in index_list:
        data_point = data_point + line_index[i]
    return data_point.replace(" ", "")

def bulk_data(line):
    detail_data["a"] = build_data_point(line, range(0, 2))
    detail_data["b"] = line[2]
    detail_data["c"] = build_data_point(line, range(3, 7))
    detail_data["d"] = build_data_point(line, range(18, 26))
    detail_data["e"] = line[26]

    # print detail_data

lines = []
for line in vision_file:
lines.append(line)

for i in range(len(lines)):
bulk_data(lines[i])

print detail_data



I am ignoring the first and last line for testing purposes. The problem is my function is returning the last element of my dictionary (in the correct format) just not the entire dictionary with every element.
If some could help me I would really appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Python file to list to dictionary

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5776
  • View blog
  • Posts: 12,587
  • Joined: 16-October 07

Re: Python file to list to dictionary

Posted 21 March 2013 - 05:14 AM

The problem is that this isn't really a dictionary:
detail_data = {
	{'a': '51', 'b': 'P ', 'c': '444', 'd': '88888888 ', 'e': 'G'},
	{'a': '89', 'b': 'U ', 'c': '333', 'd': '7777777', 'e': 'O'}
	}



What you're actually doing is loading this over and over again:
detail_data = {'a': '51', 'b': 'P ', 'c': '444', 'd': '88888888 ', 'e': 'G'}



Perhaps you want:
detail_data = [
	{'a': '51', 'b': 'P ', 'c': '444', 'd': '88888888 ', 'e': 'G'},
	{'a': '89', 'b': 'U ', 'c': '333', 'd': '7777777', 'e': 'O'}
	]



In which case:
def bulk_data(line):
	return {'a': line[0:2].replace(" ", ""), 
		'b': line[2].replace(" ", ""), 
		'c': line[3:7].replace(" ", ""), 
		'd': line[18:26].replace(" ", ""),
		'e': line[26].replace(" ", "")}

detail_data = [ bulk_data(line) for line in vision_file ]

print detail_data


This post has been edited by baavgai: 21 March 2013 - 05:14 AM

Was This Post Helpful? 1
  • +
  • -

#3 donfanzu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 08-April 12

Re: Python file to list to dictionary

Posted 21 March 2013 - 07:32 AM

Thanks very much your solution worked perfectly.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1