473,549 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

turn text lines into a list

i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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

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

Jul 19 '05 #1
8 2273
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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


str.splitlines
Jul 19 '05 #2
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);


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

chomp( my @corenames = <DATA> );

__DATA__
rb_basic_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #3
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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


txt = """rb_basic_isl amic
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile"" "

print txt.splitlines( )

Matthias
Jul 19 '05 #4
See definition of splitlines().
(http://docs.python.org/lib/string-methods.html)

-- Paul

Jul 19 '05 #5
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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


corenames = [ "rb_basic_islam ic",
"sq1_pentagonTi le",
"sq_arc501Tile" ,
"sq_arc503T ile"]

--
Grant Edwards grante Yow! TAILFINS!!...cl ick...
at
visi.com
Jul 19 '05 #6
[En-tête "Followup-To:" positionné à comp.lang.pytho n.]
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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


corenames = [ "rb_basic_islam ic",
"sq1_pentagonTi le",
"sq_arc501Tile" ,
"sq_arc503T ile"]

Another way : (less typing of quotes)

all_names = """
rb_basic_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
"""

corenames = all_names.split ()

Regards.

Jul 19 '05 #7
On 27 Jun 2005 16:56:34 GMT, "F. Petitjean" <li***********@ news.free.fr> wrote:
[En-tête "Followup-To:" positionné à comp.lang.pytho n.]
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_islami c
sq1_pentagonTil e
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corena mes);

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


corenames = [ "rb_basic_islam ic",
"sq1_pentagonTi le",
"sq_arc501Tile" ,
"sq_arc503T ile"]

Another way : (less typing of quotes)

all_names = """
rb_basic_islam ic
sq1_pentagonTi le
sq_arc501Til e
sq_arc503Til e
"""

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_islami c
sq1_pentagonTil e
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
Gunnar Hjalmarsson wrote:
@corenames=qw(
rb_basic_islami c
sq1_pentagonTil e
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_islami c
sq1_pentagonTil e
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
4263
by: Ruben | last post by:
Hello. I am trying to read a small text file using the readline statement. I can only read the first 2 records from the file. It stops at the blank lines or at lines with only spaces. I have a while statement checking for an empty string "" which I understand represents an EOF in Python. The text file has some blank lines with spaces and...
22
61350
by: Ling Lee | last post by:
Hi all. I'm trying to write a program that: 1) Ask me what file I want to count number of lines in, and then counts the lines and writes the answear out. 2) I made the first part like this: in_file = raw_input("What is the name of the file you want to open: ") in_file = open("test.txt","r")
10
3674
by: ross | last post by:
I want to do some tricky text file manipulation on many files, but have only a little programming knowledge. What are the ideal languages for the following examples? 1. Starting from a certain folder, look in the subfolders for all filenames matching *FOOD*.txt Any files matching in each folder should be copied to a new subfolder within...
13
11375
by: Richard Hollenbeck | last post by:
To prevent future apostrophe bugs and errors, isn't it just simpler to forbid an apostrophe from being entered into a text field? For example, couldn't "Alice's Restaurant" be changed to "Alices Restaurant" etc. automatically and programmatically during data entry? This would eliminate my concatinated strings from producing errors when I...
13
4165
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them from the html page (replace the words in the html page with blank space) I'm new to python and could use a little push in the right direction, any...
6
1667
by: vbfoobar | last post by:
Hello, I am looking for python code useful to process tables that are in ASCII text. The code must determine where are the columns (fields). Concerned tables for my application are various, but their columns are not very complicated to locate for a human, because even when ignoring the semantic of words, our eyes see vertical alignments
2
2136
by: flyzone | last post by:
Goodmorning people :) I have just started to learn this language and i have a logical problem. I need to write a program to parse various file of text. Here two sample: --------------- trial text bla bla bla bla error bla bla bla bla bla bla bla bla on more lines
13
1559
by: kronecker | last post by:
I am trying to delete multiple lines in a text file using the following Private Sub Read_TextFile() Dim objReader As StreamReader Dim strfull, strContents, strContentsold, strContentsnew As String objReader = New StreamReader("C:\answer.txt") 'Clear the Text Box1 TextBox1.Clear()
0
7548
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7743
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7986
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7832
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6074
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3499
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1965
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
786
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.