ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Rep:
selecting only letters of a string
hi,
I want to store each line of a text file in a list. But lines don't start from the first column. So when I want to compare some other string with them I need only its letter not the whole string with spaces.
I know this is quite easy question but I am new to python and I have thousands of questions.
thanks
And you might get better or more responses if you actually put what programming language your using in your title or at least in the beginning of your thread. I had no idea you were speaking of Python until I had to read 2/3's of your thread.
Originally posted by mohtasham1983 hi,
I want to store each line of a text file in a list. But lines don't start from the first column. So when I want to compare some other string with them I need only its letter not the whole string with spaces.
I know this is quite easy question but I am new to python and I have thousands of questions.
thanks
hmmm...well I don't fully understand what your after but maybe I can help a bit.
You can store the contents of a file in a list using list comprehension as follows:
Code:
file_list = [line[:-1] for line in open('test.txt', 'r').readlines()]
This will produce something like this:
Code:
['# Copyright 1999-2005 Gentoo Foundation', '# Distributed under the terms of the GNU General Public License v2', '# $Header:$', '', 'inherit webapp', '', 'DESCRIPTION="Jinzora is a web based media streaming and management system, designed to stream audio and video files to any internet connected computer - anytime, anywhere."', 'HOMEPAGE="http://www.jinzora.org/"', 'SRC_URI="mirror://sourceforge/jinzora/j2.0.1.tar.gz"', '', 'LICENSE="GPL-2"', 'KEYWORDS="~x86"', '', 'RDEPEND="virtual/php"', '', 'S=${WORKDIR}/${PN}2', '', 'src_install() {', '\twebapp_src_preinst', '', '\tdodoc ${S}/docs/*', '\trm -rf ${S}/docs/', '', '\tcp -a * "${D}/${MY_HTDOCSDIR}"', '', '\techo "<?php trigger_error(\'Please use install/install.php to configure phpBB for your system!\', E_USER_ERROR); ?>" > "${D}/${MY_HTDOCSDIR}/config.php"', '\twebapp_serverowned "${MY_HTDOCSDIR}/config.php"', '', '\twebapp_postinst_txt en ${FILESDIR}/2.0.10-postinstall-en.txt', '', '\twebapp_src_install', '}', '', 'http://www.gentoo.org/proj/en/glep/glep-0011.htm']
To get leading spaces out of lines you could use the strip method:
Code:
line = ' this has spaces in the front'
line = line.strip()
line
'this has spaces in the front'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.