473,802 Members | 2,015 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 2292
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
4301
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 other with blanks. Thanks a lot.
22
61419
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
3728
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 the current folder called EATING with a new name of *FOOD*COPY.txt
13
11440
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 base the string on a query. Think this is an example of the "Dreaded Apostrophe Bug." If I enter a...
13
4249
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 ideas on how to implement this? Thanks!
6
1678
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
2153
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
1579
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
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10305
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10285
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10063
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7598
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5494
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
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

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.