[Tutor] More help needed in sorting files and strings

alan.gauld@bt.com alan.gauld@bt.com
Sat, 27 Oct 2001 18:02:43 +0100


------_=_NextPart_001_01C15F09.3161EC50
Content-type: text/plain; charset="ISO-8859-1"

    > inp =open("Metar\Infometar.txt")
    > lines = inp.readlines() 
 
so far so good.
 
    > Depart = lines[1].split()[0]
 
This is OK because you know its always in line 1
 
Now lets become a bit more flexible. What we want 
is the second record which will be after the first 
blank line.
 
    current = 2
    while lines[current]:   # empty line counts as false
        current += 1
 
current will now point at the next blank line.
We want to extract the data from the first then 
second line of the record so add 1 and 2 to 
current in the indices:
 
    Destin = lines[current+1].split()[0]
    Destin1 = lines[ current+2 ].split()[0]   
    Print Depart+" "+Destin+" "+Destin1
 
You can repeat that technique until all the records
you need are found. 
 
Once you get more experienced you will discover 
better ways of working using functions which you 
define and later still, objects which can read 
themseles from files etc... But for now try 
variations on my(untested!) code above.
 
HTH,
 
Alan g
 

------_=_NextPart_001_01C15F09.3161EC50
Content-type: text/html; charset="ISO-8859-1"

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-1">


<META content="MSHTML 5.00.3013.2600" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face="Courier New"><FONT size=2>&nbsp;&nbsp;&nbsp;<SPAN 
class=720204616-27102001><FONT color=#0000ff>&nbsp;<FONT color=#000000>&gt; 
</FONT></FONT></SPAN>inp 
=open("Metar\Infometar.txt")<BR>&nbsp;&nbsp;&nbsp;&nbsp;<SPAN 
class=720204616-27102001><FONT color=#0000ff><FONT color=#000000>&gt; 
l</FONT></FONT></SPAN>ines = inp.readlines()<SPAN class=720204616-27102001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN 
class=720204616-27102001></SPAN></FONT></FONT>&nbsp;</DIV>
<DIV><FONT size=2><SPAN class=720204616-27102001><FONT color=#0000ff 
face="Courier New">so far so good.</FONT></SPAN></FONT></DIV>
<DIV><FONT face="Courier New"><FONT size=2><SPAN 
class=720204616-27102001>&nbsp;</SPAN><BR>&nbsp;<SPAN 
class=720204616-27102001><FONT color=#0000ff>&nbsp;&nbsp; 
&gt;&nbsp;</FONT></SPAN>Depart = lines[1].split()[0]<BR><SPAN 
class=720204616-27102001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>This is OK because you know its always in line 
1</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>Now lets become a bit more flexible. What we want 
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>is the second record which will be after the first 
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>blank line.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>&nbsp;&nbsp;&nbsp;&nbsp;current = 2</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>&nbsp;&nbsp;&nbsp; while lines[current]:&nbsp;&nbsp; # 
empty line counts as false</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; current += 
1</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>current will now point at the next blank 
line.</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>We want to extract the data from the first then 
</SPAN></FONT></DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001>second line </SPAN></FONT><FONT color=#0000ff 
size=2><SPAN class=720204616-27102001>of the record so add&nbsp;1 and 2 to 
</SPAN></FONT></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>current in the indices:</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New"><FONT size=2>&nbsp;&nbsp;&nbsp; Destin = 
lines<SPAN class=720204616-27102001><FONT 
color=#0000ff>[current+1</FONT></SPAN>].split()[0]<BR></FONT><FONT 
size=2>&nbsp;&nbsp;&nbsp; Destin1 = lines[<SPAN class=720204616-27102001><FONT 
color=#0000ff>&nbsp;current+2&nbsp;</FONT></SPAN>].split()[0]&nbsp;&nbsp;<SPAN 
class=720204616-27102001><FONT 
color=#0000ff>&nbsp;</FONT></SPAN></FONT></FONT></DIV>
<DIV><SPAN class=720204616-27102001></SPAN><FONT face="Courier New"><FONT 
size=2><SPAN class=720204616-27102001><FONT color=#0000ff>&nbsp;&nbsp; 
&nbsp;</FONT></SPAN>Print Depart+" "+Destin+" "+Destin1</FONT></FONT></DIV>
<DIV><FONT size=2></FONT><FONT face="Courier New">&nbsp;</FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>You can repeat that technique until all the 
records</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>you need are found. </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001>Once you get more experienced </SPAN></FONT><FONT 
color=#0000ff size=2><SPAN class=720204616-27102001>you will discover 
</SPAN></FONT></FONT></DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001>better ways of working using </SPAN></FONT><FONT 
color=#0000ff size=2><SPAN class=720204616-27102001>functions which you 
</SPAN></FONT></FONT></DIV>
<DIV><FONT face="Courier New"><FONT color=#0000ff size=2><SPAN 
class=720204616-27102001>define and later still, objects which can 
</SPAN></FONT></FONT><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>read </SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>themseles from files etc... But for now try 
</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>variations on my(untested!) code 
above.</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>HTH,</SPAN></FONT></DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT color=#0000ff face="Courier New" size=2><SPAN 
class=720204616-27102001>Alan g</SPAN></FONT></DIV>
<DIV>&nbsp;</DIV></BODY></HTML>

------_=_NextPart_001_01C15F09.3161EC50--