By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

turn text lines into a list

P: n/a
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?

Xah
xa*@xahlee.org
∑ http://xahlee.org/

Jul 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Xah Lee 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?


str.splitlines
Jul 19 '05 #2

P: n/a
Xah Lee 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
);


Impractical to mix code and data, isn't it?

chomp( my @corenames = <DATA> );

__DATA__
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #3

P: n/a
Xah Lee (27.06.2005 12:33):
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?


txt = """rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile"""

print txt.splitlines()

Matthias
Jul 19 '05 #4

P: n/a
See definition of splitlines().
(http://docs.python.org/lib/string-methods.html)

-- Paul

Jul 19 '05 #5

P: n/a
On 2005-06-27, Xah Lee <xa*@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"]

--
Grant Edwards grante Yow! TAILFINS!!...click...
at
visi.com
Jul 19 '05 #6

P: n/a
[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 <xa*@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()

Regards.

Jul 19 '05 #7

P: n/a
On 27 Jun 2005 16:56:34 GMT, "F. Petitjean" <li***********@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 <xa*@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
Jul 19 '05 #8

P: n/a
Gunnar Hjalmarsson wrote:
@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

Impractical to mix code and data, isn't it?


Obviously not impractical, given he did it quite easily and succinctly.
chomp( my @corenames = <DATA> );

__DATA__
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile


Not so easy when you have multiple variables to set. And the original
version was transparent in what it was doing - your version is not.
--
Just because I've written it doesn't mean that
either you or I have to believe it.
Jul 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.