[Baypiggies] Reading k=v format files

David Berthelot david.berthelot at gmail.com
Sat Oct 3 21:09:28 CEST 2015


The module ConfigParser in the standard library could be of help since it
seems to do exactly this:
https://docs.python.org/2/library/configparser.html

On Sat, Oct 3, 2015 at 12:05 PM, Alex Martelli via Baypiggies <
baypiggies at python.org> wrote:

> with open(authfile) as af:
>   return dict(line.strip().split('=',1) for line in af)
>
> is a 2-liner equivalent. I don't know how to make it a one-liner (except
> by removing the newline after the ':', but that's not PEP8-y:-).
>
> Alex
>
>
> On Sat, Oct 3, 2015 at 11:58 AM, Ian Zimmerman <itz at buug.org> wrote:
>
>> I have a file providing settings for both a shell script and a python
>> program.  It looks like this:
>>
>> Foo=bar
>> Foo2=baz
>>
>> and so on.  I'm looking for a short snippet (I hope for a one liner) to
>> grok the file and return a dictionary of the settings.  Trivial, you
>> say?  Yes, but this is not the first time I feel this need, and each
>> time I find myself rewritting basically the same ditty:
>>
>> def grok_auth(authfile):
>>     result=dict()
>>     with open(authfile) as af:
>>         for line in af:
>>             k, v = line.strip().split('=', 1)
>>             result[k] = v
>>     return result
>>
>> There must be a better, batteries based way.  Right?
>>
>> --
>> Please *no* private copies of mailing list or newsgroup messages.
>> Rule 420: All persons more than eight miles high to leave the court.
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> https://mail.python.org/mailman/listinfo/baypiggies
>>
>
>
> _______________________________________________
> Baypiggies mailing list
> Baypiggies at python.org
> To change your subscription options or unsubscribe:
> https://mail.python.org/mailman/listinfo/baypiggies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20151003/ff83b045/attachment-0001.html>


More information about the Baypiggies mailing list