dshimer 136
Recognized Expert New Member
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 3524 bartonc 6,596
Recognized Expert Expert
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?
dshimer 136
Recognized Expert New Member
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
bartonc 6,596
Recognized Expert Expert
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
Recognized Expert Moderator Specialist
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 time error
76. The paths are valid. I tested them and there is a file with the
extension specified.
Any help is appreciated
Marcelo Rizzo
|
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 sorted columns. to
do this at the moment i am using perl, which is very easy to do, and i
want to see if python is as easy.
so, the data i am using is some epiphyte population abundance data for
particular sites, and it looks like this:
|
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 optional
files that I could open and parse. At the end, I would easily close off
the files by iterating through the dictionary.
---< CODE FOLLOWS >---
optionalfiles = {fileAreaCode: "areacode.11", fileBuild: "build.11"}
|
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 iterate through them?
|
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
it serves up. I'd like some suggestions on good ways to do this,
because in the real world it can be quite difficult.
For example, if I'm rummaging around in a MySQL database, the table
structure and the code that generates the SQL requests...
|
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. The first column is always an English
|
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 checkbox, since
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |