Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > turn text lines into a list

Reply
Thread Tools

turn text lines into a list

 
 
Xah Lee
Guest
Posts: n/a
 
      06-27-2005
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:umper;
print Dumper(\@corenames);

----------
is there some shortcut to turn lines into list in Python?

Xah
http://www.velocityreviews.com/forums/(E-Mail Removed)
∑ http://xahlee.org/

 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      06-27-2005
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
 
Reply With Quote
 
 
 
 
Paul McGuire
Guest
Posts: n/a
 
      06-27-2005
See definition of splitlines().
(http://docs.python.org/lib/string-methods.html)

-- Paul

 
Reply With Quote
 
F. Petitjean
Guest
Posts: n/a
 
      06-27-2005
[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 <(E-Mail Removed)> 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:umper;
>> 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.

 
Reply With Quote
 
Big and Blue
Guest
Posts: n/a
 
      06-28-2005
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I turn a list of strings into a list of regexps? Mahurshi Akilla Perl Misc 5 05-14-2009 07:00 AM
Newby: how to transform text into lines of text vsoler Python 19 01-26-2009 10:38 PM
Preserve blank lines when add multiple lines of text to a cell Cah Sableng Javascript 0 04-23-2007 04:46 AM
To delete few lines and add few lines at the end of a text file using c program Murali C++ 2 03-09-2006 04:45 PM
turn text lines into a list Xah Lee Python 8 06-28-2005 08:57 PM



Advertisments