LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-22-2005, 08:03 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
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
 
Old 06-22-2005, 08:12 AM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
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.
 
Old 06-22-2005, 01:44 PM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
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'
 
Old 06-22-2005, 03:48 PM   #4
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Thank you man, you were quite helpful
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash way to tell if String is in String tongar Programming 3 06-16-2005 06:59 AM
getting greek letters? buffed317 Linux - General 2 09-11-2004 02:43 PM
Why are the letters so far apart in MandrakeLinux 10.0 WanabeLinus Mandriva 2 07-18-2004 12:06 PM
C....Search a string for a string Scrag Programming 4 06-14-2004 04:15 PM
java test if string in string array is null. exodist Programming 3 02-21-2004 01:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration