Re: Variable + String Format

2009-02-10 Thread Joel Ross

Joel Ross wrote:

Hi all,

I have this piece of code:
#

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line 
e.g. test would only print t and readline() does the same thing.


I have also tried readlines() which gives me the result:

test
.com

The file Wordlist has one word on each line for example

test
test1
test2

I need it to loop though the Wordlist file and print/return the results

test.com
test1.com
test2.com

I'm just looking for a point in the right direction.

Much Thanks

JOelC



THAT'S BETTER!!! :)
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-10 Thread Joel Ross

Steven D'Aprano wrote:

On Wed, 11 Feb 2009 18:52:40 +1100, Joel Ross wrote:


Thanks for the quick response guys. Help me out a alot. I'm a newbie to
python and your replies help me understand a bit more about python!!


Joel, unless you have Guido's time machine and are actually posting from 
the future, the clock, or possibly the time zone, on your PC is set 
wrong. 




I'm from the distant future. Too protect and Serve humankind!!!

lol thanks dude. I'm a day ahead of myself. I had my linux box running 
off UTC and it didn't include daylight savings so I adjusted it myself, 
must have accidentally changed the day as well :).

--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-10 Thread Steven D'Aprano
On Wed, 11 Feb 2009 18:52:40 +1100, Joel Ross wrote:

> Thanks for the quick response guys. Help me out a alot. I'm a newbie to
> python and your replies help me understand a bit more about python!!

Joel, unless you have Guido's time machine and are actually posting from 
the future, the clock, or possibly the time zone, on your PC is set 
wrong. 


-- 
Steven



--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-09 Thread Joel Ross

Joel Ross wrote:

Hi all,

I have this piece of code:
#

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

for line in file.read():
if not line: break
print line + '.com '
return line



readLines()
file.close()

##

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line 
e.g. test would only print t and readline() does the same thing.


I have also tried readlines() which gives me the result:

test
.com

The file Wordlist has one word on each line for example

test
test1
test2

I need it to loop though the Wordlist file and print/return the results

test.com
test1.com
test2.com

I'm just looking for a point in the right direction.

Much Thanks

JOelC



Thanks for the quick response guys. Help me out a alot. I'm a newbie to 
python and your replies help me understand a bit more about python!!


Thank You
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-09 Thread Gabriel Genellina

En Tue, 10 Feb 2009 08:03:06 -0200, Joel Ross  escribió:


#

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

 for line in file.read():
 if not line: break
 print line + '.com '
 return line



readLines()
file.close()

##

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line  
e.g. test would only print t and readline() does the same thing.


This should print every line in the file, adding .com at the end:

wordList = "/tmp/Wordlist"
with open(wordList, 'r') as wl:
  for line in wl:
print line.rstrip() + '.com '

Note that:
- I iterate over the file self; files are their own line-iterators.
- I've used the 'r' mode instead (I assume it's a text file because you  
read it line by line, and as you don't update it, the '+' isn't required)
- the line read includes the end-of-line '\n' at the end; rstrip() removes  
it and any trailing whitespace. If you don't want this, use rstrip('\n')

- I've used the with statement to ensure the file is closed at the end


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-09 Thread Gabriel Genellina

En Tue, 10 Feb 2009 08:03:06 -0200, Joel Ross  escribió:


#

wordList = "/tmp/Wordlist"
file = open(wordList, 'r+b')


def readLines():

 for line in file.read():
 if not line: break
 print line + '.com '
 return line



readLines()
file.close()

##

It returns the results:

t.com

NOTE: Only returns the first letter of the first word on the first line  
e.g. test would only print t and readline() does the same thing.


This should print every line in the file, adding .com at the end:

wordList = "/tmp/Wordlist"
with open(wordList, 'r') as wl:
  for line in wl:
print line.rstrip() + '.com '

Note that:
- I iterate over the file self; files are their own line-iterators.
- I've used the 'r' mode instead (I assume it's a text file because you  
read it line by line, and as you don't update it, the '+' isn't required)
- the line read includes the end-of-line '\n' at the end; rstrip() removes  
it and any trailing whitespace. If you don't want this, use rstrip('\n')

- I've used the with statement to ensure the file is closed at the end


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-09 Thread John Machin
On Feb 9, 9:13 pm, Chris Rebert  wrote:
> On Tue, Feb 10, 2009 at 2:03 AM, Joel Ross  wrote:
> > Hi all,
>
> > I have this piece of code:
> > #
>
> > wordList = "/tmp/Wordlist"
> > file = open(wordList, 'r+b')
>
> Why are you opening the file in binary mode? It's content is text!
>
>
>
> > def readLines():
>
> >        for line in file.read():
>
> .read() reads the *entire* file into a string. When you iterate over a
> string, such as in a for-loop, you get *individual characters*,
> whereas you appear to want lines of text. To go line-by-line, use `for
> line in file` instead (note: `line` will include the trailing newline
> at the end of each line).
> And don't use `file` as a variable name; it shadows the name of the
> buitlin type.
>

Changed as suggested above, with further comments:

wordList = "/tmp/Wordlist"
f = open(wordList, 'r')
def readLines():
## There is no point in putting the next four lines
## inside a function if you are going to call the
## function (a) immediately and (b) once only.
## All it does is provide scope for unwanted complications.
   for line in f:
line = line.rstrip('\n')
print line + '.com '
## Complications like this next line; why is it returning "line"??
## And why is it indented like that?
## It will cause only the first line to be read.
return line
## Maybe you you meant it to be here:
return
## But then it's redundant because a function returns anyway
## when flow of control would fall off the bottom.
readLines()
f.close()

HTH,
John
--
http://mail.python.org/mailman/listinfo/python-list


Re: Variable + String Format

2009-02-09 Thread Chris Rebert
On Tue, Feb 10, 2009 at 2:03 AM, Joel Ross  wrote:
> Hi all,
>
> I have this piece of code:
> #
>
> wordList = "/tmp/Wordlist"
> file = open(wordList, 'r+b')

Why are you opening the file in binary mode? It's content is text!

>
> def readLines():
>
>for line in file.read():

.read() reads the *entire* file into a string. When you iterate over a
string, such as in a for-loop, you get *individual characters*,
whereas you appear to want lines of text. To go line-by-line, use `for
line in file` instead (note: `line` will include the trailing newline
at the end of each line).
And don't use `file` as a variable name; it shadows the name of the
buitlin type.

Cheers,
Chris

-- 
Follow the path of the Iguana...
http://rebertia.com
--
http://mail.python.org/mailman/listinfo/python-list