Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

reading from a text file

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


sanneh27 at hotmail

Nov 26, 2009, 4:08 PM

Post #1 of 7 (297 views)
Permalink
reading from a text file

hi all

i would like to create a python program that would read from a text file and returns one result at random.
e.g
in the text file i have these data

1.hello
2.my name
3.is
4.World

Your help is highly appreciated..thnx in advance

$LIM $H [at] D


_________________________________________________________________
Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you.
http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010


olof.bjarnason at gmail

Nov 30, 2009, 5:05 AM

Post #2 of 7 (286 views)
Permalink
Re: reading from a text file [In reply to]

2009/11/27 baboucarr sanneh <sanneh27 [at] hotmail>:
> hi all
>
> i would like to create a python program that would read from a text file and
> returns one result at random.
> e.g
> in the text file i have these data
>
> 1.hello
> 2.my name
> 3.is
> 4.World
>
> Your help is highly appreciated..thnx in advance

Hi babourarr;

import random
with open("c:/test.txt") as f:
lines = f.read().splitlines()
random_line = lines[random.randrange(len(lines))]
print(random_line)

>
> $LIM $H [at] D
>
>
> ________________________________
> Windows Live: Friends get your Flickr, Yelp, and Digg updates when they
> e-mail you.
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



--
twitter.com/olofb
olofb.wordpress.com
olofb.wordpress.com/tag/english
--
http://mail.python.org/mailman/listinfo/python-list


simon at brunningonline

Nov 30, 2009, 5:06 AM

Post #3 of 7 (287 views)
Permalink
Re: reading from a text file [In reply to]

2009/11/27 baboucarr sanneh <sanneh27 [at] hotmail>:
> hi all
>
> i would like to create a python program that would read from a text file and
> returns one result at random.

This might be of use:

http://code.activestate.com/recipes/426332/#c2

--
Cheers,
Simon B.
--
http://mail.python.org/mailman/listinfo/python-list


ebonak at hotmail

Nov 30, 2009, 5:34 AM

Post #4 of 7 (288 views)
Permalink
Re: reading from a text file [In reply to]

baboucarr sanneh wrote:
>
> i would like to create a python program that would read from a text file
> and returns one result at random.


#!/usr/bin/env python

# assuming the file fits into memory, and you are interested in
# random lines

from random import randrange

f = open('data.txt')
data = f.readlines()
number = len(data)
pick = randrange(0, number)
print data[pick]


should do the trick.

Esmail

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


mail at timgolden

Nov 30, 2009, 7:41 AM

Post #5 of 7 (290 views)
Permalink
Re: reading from a text file [In reply to]

Olof Bjarnason wrote:
> 2009/11/27 baboucarr sanneh <sanneh27 [at] hotmail>:
>> hi all
>>
>> i would like to create a python program that would read from a text file and
>> returns one result at random.
>> e.g
>> in the text file i have these data
>>
>> 1.hello
>> 2.my name
>> 3.is
>> 4.World
>>
>> Your help is highly appreciated..thnx in advance
>
> Hi babourarr;
>
> import random
> with open("c:/test.txt") as f:
> lines = f.read().splitlines()
> random_line = lines[random.randrange(len(lines))]
> print(random_line)

Or, slightly more simply:

import random
with open ("c:/test.txt") as f:
print random.choice (list (f))


You need the list () because random.choice only works
on a finite iterable.

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


inhahe at gmail

Nov 30, 2009, 9:48 AM

Post #6 of 7 (288 views)
Permalink
Re: reading from a text file [In reply to]

i don't understand the point of using 'with'
but i don't understand what 'with' does at all
i've tried to understand it a few times
anyway here:

import random
result = random.choice(open("c:\\test.txt").readlines())

On Mon, Nov 30, 2009 at 10:41 AM, Tim Golden <mail [at] timgolden> wrote:
> Olof Bjarnason wrote:
>>
>> 2009/11/27 baboucarr sanneh <sanneh27 [at] hotmail>:
>>>
>>> hi all
>>>
>>> i would like to create a python program that would read from a text file
>>> and
>>> returns one result at random.
>>> e.g
>>> in the text file i have these data
>>>
>>> 1.hello
>>> 2.my name
>>> 3.is
>>> 4.World
>>>
>>> Your help is highly appreciated..thnx in advance
>>
>> Hi babourarr;
>>
>> import random
>> with open("c:/test.txt") as f:
>>  lines = f.read().splitlines()
>> random_line = lines[random.randrange(len(lines))]
>> print(random_line)
>
> Or, slightly more simply:
>
> import random
> with open ("c:/test.txt") as f:
>  print random.choice (list (f))
>
>
> You need the list () because random.choice only works
> on a finite iterable.
>
> TJG
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


mail at timgolden

Nov 30, 2009, 10:34 AM

Post #7 of 7 (288 views)
Permalink
Re: reading from a text file [In reply to]

inhahe wrote:
> i don't understand the point of using 'with'
> but i don't understand what 'with' does at all
> i've tried to understand it a few times
> anyway here:
>
> import random
> result = random.choice(open("c:\\test.txt").readlines())


Yep. That'll do the trick. The point of "with" is that,
while in CPython, an open file will be closed as soon
as it is out of scope -- in this case, after that line --
in other implementations of Python, this may not be so.
Good practice suggests using with (or try/finally) which
ensures that the file is closed, regardless of the implementation.

For a quick example it's not always easy to know whether
to include this kind of detail, but a with: block adds
very little noise and will save you from being caught out
somewhere, someday.

http://www.python.org/doc/current/whatsnew/2.6.html#pep-343-the-with-statement

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

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.