<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Jay Loden wrote:
<blockquote cite="mid200501212346.41349.python@jayloden.com" type="cite">
  <pre wrap="">One simple solution is to do: 

fle = open(file)
contents = file.readlines()
file.close()
print contents[x]  #or store this in a variable, whatever

-Jay

On Friday 21 January 2005 11:22, J. M. Strother wrote:
  </pre>
  <blockquote type="cite">
    <pre wrap="">I have  a text file containing 336 records.
I can read and print out the whole file without any problem.
What I want to do is read and print out one record only (chosen at
random). So I need to get to record x, select it, and then print it (or
store it in a variable).
Can anyone tell me now to do this?

I'm new to Python and programming, so sorry if this is very basic. Thanks.

jon
_______________________________________________
Tutor maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a>
    </pre>
  </blockquote>
  <pre wrap=""><!---->_______________________________________________
Tutor maillist  -  <a class="moz-txt-link-abbreviated" href="mailto:Tutor@python.org">Tutor@python.org</a>
<a class="moz-txt-link-freetext" href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</a></pre>
</blockquote>
Whoops! Sorta took the hard way on that one. <tt><font color="#666666"><b>readlines()</b></font>
</tt>is by all means a much better way of doing this.<br>
<pre class="moz-signature" cols="72">-- 
Email: singingxduck AT gmail DOT com
AIM: singingxduck
Programming Python for the fun of it.</pre>
</body>
</html>