turn text lines into a list
Bengt Richter
bokr at oz.net
Mon Jun 27 13:37:32 EDT 2005
On 27 Jun 2005 16:56:34 GMT, "F. Petitjean" <littlejohn.75 at news.free.fr> wrote:
>[En-tête "Followup-To:" positionné à comp.lang.python.]
>Le Mon, 27 Jun 2005 14:27:28 -0000, Grant Edwards a écrit :
>> On 2005-06-27, Xah Lee <xah at xahlee.org> wrote:
>>> i have a large number of lines i want to turn into a list.
>>> In perl, i can do
>>>
>>> @corenames=qw(
>>> rb_basic_islamic
>>> sq1_pentagonTile
>>> sq_arc501Tile
>>> sq_arc503Tile
>>> );
>>>
>>> use Data::Dumper;
>>> print Dumper(\@corenames);
>>>
>>> ----------
>>> is there some shortcut to turn lines into list in Python?
>>
>> corenames = [ "rb_basic_islamic",
>> "sq1_pentagonTile",
>> "sq_arc501Tile",
>> "sq_arc503Tile"]
>>
>Another way : (less typing of quotes)
>
>all_names = """
>rb_basic_islamic
>sq1_pentagonTile
>sq_arc501Tile
>sq_arc503Tile
>"""
>
>corenames = all_names.split()
Of course, the lines better not have embedded spaces or they'll be split
into several lines. For lines per se, probably I'd do
corenames = """\
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
""".splitlines()
Note the \ to avoid a blank leading line.
>>> """\
... solid
... embedded space
... leading
... trailing
... both
... """.splitlines()
['solid', 'embedded space', ' leading', 'trailing ', ' both ']
Regards,
Bengt Richter
More information about the Python-list
mailing list