Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: How to create a dict based on such a file?

Reply
Thread Tools

Re: How to create a dict based on such a file?

 
 
Dan Stromberg
Guest
Posts: n/a
 
      02-14-2011
On Sun, Feb 13, 2011 at 9:47 PM, Wang Coeus <(E-Mail Removed)> wrote:
> Hi all,
> I am new to python. Currently I encountered a problem, please help me to
> solve this. Thanks in advance!
> I have a file like below:
> ++++++++++++++++++++++++++++++++++++++
> block1
> {
> *key1=value1
> *key2=value2
> *key3=value3
> }
> block2
> {
> *key1=value4
> *key2=value5
> *key4=value6
> }
> ...
> blockn
> {
> *key1=value7
> *key2=value8
> *keyn=valuen
> }
> +++++++++++++++++++++++++++++++++++++++
> Different block may have different keys and even same key in different
> blocks may have different values.
>
> Now I want to get a function, which like this:
> func(key)
> and it will return a dictionary as below:
> func(key1) = [block1:value1,block2:value4,...,blockn:value7]
> and if one block has no "key1" parameter, it will not include in this
> dict.
>
> Thanks a lot!


pyparsing should be able to make pretty short work of exactly the
format you want.

FWIW, the creator of the many .ini format(s), Microsoft, no longer
recommends using .ini files.
 
Reply With Quote
 
 
 
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-14-2011
On Mon, 14 Feb 2011 11:25:00 -0800, Dan Stromberg wrote:

> FWIW, the creator of the many .ini format(s), Microsoft, no longer
> recommends using .ini files.


That's because they want people to use the registry.

INI files are simple, easy to parse, lightweight, and human readable and
human writable using nothing more than a text editor. For many purposes,
they are perfectly fine. As I see it, the biggest problems with INI files
are:

* the INI file module that comes with Python is quite primitive;

* there are many slightly different behaviours you might want in an INI
file, and no clean or obvious way to tell which one you are dealing with
just from the file.

E.g. if you repeat a key twice, does the second line override the first,
or add a second value, or is it an error?

INI files aren't suitable for everything, but there's no need to avoid
them just because Microsoft don't want you using them, or because they're
uncool or something...

Rant: what I *really hate* is when people use XML just because XML is the
in-thing, not because they need it. Instead of:

[main]
key = value
food = spam
colour = green


you get something like this:


<?xml version="1.0"?>
<main>
<entry name="key" value="value"></entry>
<entry name="food" value="spam"></entry>
<entry name="colour" value="green"></entry>
</main>


--
Steven
 
Reply With Quote
 
 
 
 
Dan Stromberg
Guest
Posts: n/a
 
      02-15-2011
On Mon, Feb 14, 2011 at 3:06 PM, Steven D'Aprano
<(E-Mail Removed)> wrote:
> On Mon, 14 Feb 2011 11:25:00 -0800, Dan Stromberg wrote:
>
>> FWIW, the creator of the many .ini format(s), Microsoft, no longer
>> recommends using .ini files.

>
> That's because they want people to use the registry.
>
> INI files are simple, easy to parse, lightweight, and human readable and
> human writable using nothing more than a text editor. For many purposes,
> they are perfectly fine. As I see it, the biggest problems with INI files
> are:
>
> * the INI file module that comes with Python is quite primitive;
>
> * there are many slightly different behaviours you might want in an INI
> file, and no clean or obvious way to tell which one you are dealing with
> just from the file.


More to the point, there are many dialects of ini files, and nothing
more than a defacto standard for how they should work.

They also stand out like a sore thumb on anything but Windows, and
look a bit anachronistic on Windows.

But I don't hate ini files - I just think they aren't that clearly a
great choice. If you want to use them, fine. If you recommend them,
fine.
 
Reply With Quote
 
alex23
Guest
Posts: n/a
 
      02-15-2011
On Feb 15, 9:06*am, Steven D'Aprano <steve
(E-Mail Removed)> wrote:
> As I see it, the biggest problems with INI files are:
>
> * the INI file module that comes with Python is quite primitive;
>
> * there are many slightly different behaviours you might want in an INI
> file, and no clean or obvious way to tell which one you are dealing with
> just from the file.


[...]

> Rant: what I *really hate* is when people use XML just because XML is the
> in-thing, not because they need it.


Could it possibly be then that people use XML because a) the support
is better and b) the behaviour is more predictable? That sounds more
like good tool choice than faddism.
 
Reply With Quote
 
Westley Martínez
Guest
Posts: n/a
 
      02-15-2011
On Mon, 2011-02-14 at 19:41 -0800, alex23 wrote:
> On Feb 15, 9:06 am, Steven D'Aprano <steve
> (E-Mail Removed)> wrote:
> > As I see it, the biggest problems with INI files are:
> >
> > * the INI file module that comes with Python is quite primitive;
> >
> > * there are many slightly different behaviours you might want in an INI
> > file, and no clean or obvious way to tell which one you are dealing with
> > just from the file.

>
> [...]
>
> > Rant: what I *really hate* is when people use XML just because XML is the
> > in-thing, not because they need it.

>
> Could it possibly be then that people use XML because a) the support
> is better and b) the behaviour is more predictable? That sounds more
> like good tool choice than faddism.

But why is the support better? ... faddism.

 
Reply With Quote
 
Dan Stromberg
Guest
Posts: n/a
 
      02-15-2011
On Mon, Feb 14, 2011 at 8:31 PM, Westley Martínez <(E-Mail Removed)> wrote:
> On Mon, 2011-02-14 at 19:41 -0800, alex23 wrote:
>> On Feb 15, 9:06 am, Steven D'Aprano <steve
>> (E-Mail Removed)> wrote:
>> > As I see it, the biggest problems with INI files are:
>> >
>> > * the INI file module that comes with Python is quite primitive;
>> >
>> > * there are many slightly different behaviours you might want in an INI
>> > file, and no clean or obvious way to tell which one you are dealing with
>> > just from the file.

>>
>> [...]
>>
>> > Rant: what I *really hate* is when people use XML just because XML is the
>> > in-thing, not because they need it.

>>
>> Could it possibly be then that people use XML because a) the support
>> is better and b) the behaviour is more predictable? That sounds more
>> like good tool choice than faddism.

> But why is the support better? ... faddism.


Well...:
1) XML was one of the first such interchange formats, so it naturally
got some press; it was an idea whose time had come. It's widely
accepted that sometimes a first mover gains an advantage over
follow-on products, even if it's only temporary.
2) XML is highly general
3) XML doesn't suffer from the weird exceptions to general rules that
something like YAML has

But in the future, I'll likely lean more on JSON.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      02-15-2011
On Mon, 14 Feb 2011 19:41:00 -0800, alex23 wrote:

> On Feb 15, 9:06Â*am, Steven D'Aprano <steve
> (E-Mail Removed)> wrote:
>> As I see it, the biggest problems with INI files are:
>>
>> * the INI file module that comes with Python is quite primitive;
>>
>> * there are many slightly different behaviours you might want in an INI
>> file, and no clean or obvious way to tell which one you are dealing
>> with just from the file.

>
> [...]
>
>> Rant: what I *really hate* is when people use XML just because XML is
>> the in-thing, not because they need it.

>
> Could it possibly be then that people use XML because a) the support is
> better and b) the behaviour is more predictable? That sounds more like
> good tool choice than faddism.



Of course it *could* be, but is it?

I'm certainly not denying that XML can be useful for many applications,
nor am I saying that INI files are sufficient for everything. But if all
you need is:

key = value

then why use this?

<entry name="key" value="value"></entry>

What benefit does it give? It just increases the number of things that
can go wrong.


--
Steven
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How to create a dict based on such a file? Wang Coeus Python 2 02-14-2011 10:10 AM
dict.has_key(x) versus 'x in dict' Paul Melis Python 48 12-15-2006 05:55 PM
dict!ident as equivalent of dict["ident"] Alexander Kozlovsky Python 5 05-22-2006 08:06 AM
Re: dict->XML->dict? Or, passing small hashes through text? Skip Montanaro Python 0 08-15-2003 03:46 PM



Advertisments