[Baypiggies] Reading k=v format files

Alex Martelli aleax at google.com
Sat Oct 3 21:05:09 CEST 2015


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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/baypiggies/attachments/20151003/6a0f36e6/attachment.html>


More information about the Baypiggies mailing list