I have a file whose structure in strictly generic terms is similar to the following. - keyname first
-
keyword1 1.1
-
keyword2 1.2
-
keyword3 1.3
-
keyname second
-
keyword1 2.1
-
keyword2 2.2
-
keyword3 2.3
-
keyname third
-
keyword1 3.1
-
keyword2 3.2
-
keyword3 3.3
keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like - >>> f=open('/tmp/test.txt','r')
-
>>> d=f.readlines()
-
>>> for l in d:
-
... print l.split()
-
...
-
['keyname', 'first']
-
['keyword1', '1.1']
-
['keyword2', '1.2']
-
['keyword3', '1.3']
-
['keyname', 'second']
-
['keyword1', '2.1']
-
['keyword2', '2.2']
-
['keyword3', '2.3']
-
['keyname', 'third']
-
['keyword1', '3.1']
-
['keyword2', '3.2']
-
['keyword3', '3.3']
What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like - dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
allowing for access to whole keys, or individual data values like - >>> dict['second']
-
{'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
-
>>> dict['second']['keyword2']
-
2.2000000000000002
This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
4 3498
I have a file whose structure in strictly generic terms is similar to the following. - keyname first
-
keyword1 1.1
-
keyword2 1.2
-
keyword3 1.3
-
keyname second
-
keyword1 2.1
-
keyword2 2.2
-
keyword3 2.3
-
keyname third
-
keyword1 3.1
-
keyword2 3.2
-
keyword3 3.3
keyname value is always going to contain the name of a data set identified by keywords and values. The keywords are always the same for each keyname and contain data which is unique to the keyname. In the simplest terms if I read it like - >>> f=open('/tmp/test.txt','r')
-
>>> d=f.readlines()
-
>>> for l in d:
-
... print l.split()
-
...
-
['keyname', 'first']
-
['keyword1', '1.1']
-
['keyword2', '1.2']
-
['keyword3', '1.3']
-
['keyname', 'second']
-
['keyword1', '2.1']
-
['keyword2', '2.2']
-
['keyword3', '2.3']
-
['keyname', 'third']
-
['keyword1', '3.1']
-
['keyword2', '3.2']
-
['keyword3', '3.3']
What I would like to do is build a dictionary in which each keyname has a value which is another dictionary made up of keywords and values. For example if I were to manually build it the dictionary would look like - dict={'first':{'keyword1':1.1,'keyword2':1.2,'keyword3':1.3},'second':{'keyword1':2.1,'keyword2':2.2,'keyword3':2.3},'third':{'keyword1':3.1,'keyword2':3.2,'keyword3':3.3}}
allowing for access to whole keys, or individual data values like - >>> dict['second']
-
{'keyword3': 2.2999999999999998, 'keyword2': 2.2000000000000002, 'keyword1': 2.1000000000000001}
-
>>> dict['second']['keyword2']
-
2.2000000000000002
This is ripe for iterating over the data and adding as I go, if it were a list I would append, but I don't use dictionaries very often and don't know how to add/append/insert data. What is the best way to do this?
It's simple: - aDict[newKeyName] = newValue
Scary, huh?
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.
Thanks
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.
Thanks
Any time, D. It's really no trouble (and you contribute so much that the simplest to the toughest questions are yours for the asking). And as you say, it could help someone else along the way.
bvdet 2,851
Expert Mod 2GB
Good grief, I knew it had to be easy. I would be sorry for taking your time instead of digging through a book, but I guess it will be a good reference in case anybody else missed it. Python data types are just too cool and I'm finding that I like dictionaries more than I thought I would when I first started studying them.
Thanks
I like dictionaries also. I hope this helps you: - data = open(fn).read()
-
dataLst = [i.strip() for i in data.split('keyname') if i != '']
-
dd = {}
-
for item in dataLst:
-
itemLst = item.split('\n')
-
dd[itemLst[0]] = dict(zip([i.split()[0] for i in itemLst[1:]], [j.split()[1] for j in itemLst[1:]]))
-
-
for key in dd:
-
print '%s = %s' % (key, dd[key])
-
-
'''
-
>>> second = {'keyword3': '2.3', 'keyword2': '2.2', 'keyword1': '2.1'}
-
third = {'keyword3': '3.3', 'keyword2': '3.2', 'keyword1': '3.1'}
-
first = {'keyword3': '1.3', 'keyword2': '1.2', 'keyword1': '1.1'}
-
>>>
-
'''
I also apreciate your contributions. :)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Marcelo Rizzo |
last post by:
I am trying to get the name of a file with a specific extension (tmw)
from several different directories. The problem I am having is that
the program stops working on the second pass with an run...
|
by: ben moretti |
last post by:
hi
i'm learning python, and one area i'd use it for is data management in
scientific computing. in the case i've tried i want to reformat a data
file from a normalised list to a matrix with some...
|
by: Julian Yap |
last post by:
Hi all,
I'm trying to get some ideas on the best way to do this.
In this particular coding snippet, I was thinking of creating a
dictionary of file objects and file names. These would be...
|
by: supercomputer |
last post by:
I am using this function to parse data I have stored in an array.
This is what the array looks like:
, , , , , , , , ,
, ,
, , , , , , ,
, , , , , ]
This is the code to parse the array:
|
by: Djam |
last post by:
Hi,
I need your help to build a dictionary on mysal database to have an
efficient keyword search engine.
Thanks :-)
Djam
|
by: jaso |
last post by:
Hi,
If have a structure of a database record like this:
struct record {
char id;
char title;
...
};
Is there some way to find out how many member variables there is
in the struct and then...
|
by: Frank van Wensveen |
last post by:
Friend, coders, fellow wage slaves, lend my your ears.
I believe that in a perfect world the design of a website (or feature
on a website) should be totally separated from its design and the data...
|
by: liujiaping |
last post by:
Hi, all.
I have a dictionary-like file which has the following format:
first 4
column 7
is 9
a 23
word 134
....
Every line has two columns....
|
by: tdahsu |
last post by:
I have twenty-five checkboxes I need to create (don't ask):
self.checkbox1 = ...
self.checkbox2 = ...
..
..
..
self.checkbox25 = ...
Right now, my code has 25 lines in it, one for each...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |