LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   selecting only letters of a string (https://www.linuxquestions.org/questions/programming-9/selecting-only-letters-of-a-string-336063/)

mohtasham1983 06-22-2005 08:03 AM

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

trickykid 06-22-2005 08:12 AM

Moved: More suitable in our Programming forum.

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.

Crashed_Again 06-22-2005 01:44 PM

Re: selecting only letters of a string
 
Quote:

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'


mohtasham1983 06-22-2005 03:48 PM

Thank you man, you were quite helpful :)


All times are GMT -5. The time now is 09:42 AM.