Python论坛  - 讨论区

标题:[python-chinese] string to dict

2006年01月23日 星期一 15:52

cry zyqmail at tom.com
Mon Jan 23 15:52:01 HKT 2006

python,您好!

如下的字符串:
"""
    ABC:123
    DEF:456
    ...
"""
怎么方便的转换成字典?PYTHON下什么方法最简单呢?

谢谢

                    致
礼!

            cry
            zyqmail at tom.com



[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月23日 星期一 15:56

Zoom Quiet zoom.quiet at gmail.com
Mon Jan 23 15:56:24 HKT 2006

dict = {}
for line in string:
   item = line.split(":")
   dict[item[0]]=item[1]

在 06-1-23,cry<zyqmail at tom.com> 写道:
> python,您好!
>
> 如下的字符串:
> """
>     ABC:123
>     DEF:456
>     ...
> """
> 怎么方便的转换成字典?PYTHON下什么方法最简单呢?
>
> 谢谢
>
>> 礼!
>
>             cry
>             zyqmail at tom.com
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>


--
"""Time is unimportant, only life important!
my blog:  http://blog.zoomquiet.org/
my wiki:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
my book:  http://www.douban.com/group/zoomquiet/
"""

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月23日 星期一 16:12

Slowness Chen chenzh at bhh.com.cn
Mon Jan 23 16:12:15 HKT 2006

dict(line.split(':') for line in astring.split('\n'))
----- Original Message ----- 
From: "cry" <zyqmail at tom.com>
To: "python" <python-chinese at lists.python.cn>
Sent: Monday, January 23, 2006 3:00 PM
Subject: [python-chinese] string to dict


python,您好!

如下的字符串:
"""
    ABC:123
    DEF:456
    ...
"""
怎么方便的转换成字典?PYTHON下什么方法最简单呢?

谢谢

                    致
礼!

            cry
            zyqmail at tom.com


_______________________________________________
python-chinese
Post: send python-chinese at lists.python.cn
Subscribe: send subscribe to python-chinese-request at lists.python.cn
Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
Detail Info: http://python.cn/mailman/listinfo/python-chinese


[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月23日 星期一 16:15

Zoom Quiet zoom.quiet at gmail.com
Mon Jan 23 16:15:59 HKT 2006

在 06-1-23,Slowness Chen<chenzh at bhh.com.cn> 写道:
> dict(line.split(':') for line in astring.split('\n'))
great!

越来越 Pythonic 了

> ----- Original Message -----
> From: "cry" <zyqmail at tom.com>
> To: "python" <python-chinese at lists.python.cn>
> Sent: Monday, January 23, 2006 3:00 PM
> Subject: [python-chinese] string to dict
>
>
> python,您好!
>
> 如下的字符串:
> """
>     ABC:123
>     DEF:456
>     ...
> """
> 怎么方便的转换成字典?PYTHON下什么方法最简单呢?
>
> 谢谢
>

--
"""Time is unimportant, only life important!
my blog:  http://blog.zoomquiet.org/
my wiki:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
my book:  http://www.douban.com/group/zoomquiet/
"""

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月23日 星期一 16:39

杨斌斌 yangbb at gmail.com
Mon Jan 23 16:39:49 HKT 2006

string="""
abc:123
def:456
ghi:789
"""
dict={}
string=string[1:]
string.splitlines()
for item in string:
     item=item.split(':')
     dict.setdefault(item[0],item[1])



2006/1/23, Zoom Quiet <zoom.quiet at gmail.com>:
>
> dict = {}
> for line in string:
>   item = line.split(":")
>   dict[item[0]]=item[1]
>
> 在 06-1-23,cry<zyqmail at tom.com> 写道:
> > python,您好!
> >
> > 如下的字符串:
> > """
> >     ABC:123
> >     DEF:456
> >     ...
> > """
> > 怎么方便的转换成字典?PYTHON下什么方法最简单呢?
> >
> > 谢谢
> >
> >                     致
> > 礼!
> >
> >             cry
> >             zyqmail at tom.com
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
>
>
> --
> """Time is unimportant, only life important!
> my blog:  http://blog.zoomquiet.org/
> my wiki:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
> my book:  http://www.douban.com/group/zoomquiet/
> """
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>
>


--
祝你开心
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060123/742e5466/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年01月23日 星期一 16:41

杨斌斌 yangbb at gmail.com
Mon Jan 23 16:41:30 HKT 2006

在06-1-23,杨斌斌 <yangbb at gmail.com> 写道:
>
> string="""
> abc:123
> def:456
> ghi:789
> """
> dict={}
> string=string[1:]
> string=string.splitlines()
> for item in string:
>      item=item.split(':')
>      dict.setdefault(item[0],item[1])
>
>
>
> 2006/1/23, Zoom Quiet <zoom.quiet at gmail.com>:
> >
> > dict = {}
> > for line in string:
> >   item = line.split(":")
> >   dict[item[0]]=item[1]
> >
> > 在 06-1-23,cry< zyqmail at tom.com> 写道:
> > > python,您好!
> > >
> > > 如下的字符串:
> > > """
> > >     ABC:123
> > >     DEF:456
> > >     ...
> > > """
> > > 怎么方便的转换成字典?PYTHON下什么方法最简单呢?
> > >
> > > 谢谢
> > >
> > >                     致
> > > 礼!
> > >
> > >             cry
> > >             zyqmail at tom.com
> > >
> > >
> > > _______________________________________________
> > > python-chinese
> > > Post: send python-chinese at lists.python.cn
> > > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > > Unsubscribe: send unsubscribe to
> > python-chinese-request at lists.python.cn
> > > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> > >
> >
> >
> > --
> > """Time is unimportant, only life important!
> > my blog:  http://blog.zoomquiet.org/
> > my wiki:   http://wiki.woodpecker.org.cn/moin/ZoomQuiet
> > my book:  http://www.douban.com/group/zoomquiet/
> > """
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> >
> > Detail Info: http://python.cn/mailman/listinfo/python-chinese
> >
> >
>
>
> --
> 祝你开心




--
祝你开心
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060123/cedcf55e/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号