Stuck
Simon
tomega at earthlink.net
Wed May 17 17:06:25 EDT 2000
You need something like this:
import string
# Prompt for a filname.
filename = raw_input("Enter filename: ")
# Open the file.
f = open(filename, "r")
while 1:
line = f.readline()
if not line:
break
line_parts = string.split(line)
if line_parts[0] == "H3":
print "Region:\t%s" % line_parts[1]
print "District:\t%s" % line_parts[2]
f.close()
--S.
Fergus M Hayman wrote:
> Hi
> Trying to process a file and format it for printing.
>
> As such
>
> import string
> f = raw_input('Enter file : ')
> line = f.readline(f)
> while line:
> for i in line:
> line = f.readline()
> if line == "H3':
> print " Region", '\t:', [0:] # Extract
> first item in line H3 ??
> print " District", "\t", [1:]
>
> My infile looks like this from a handheld device
>
> H1: 9085, FERGUS, HAYMAN
> H2: 71G, NORTHVIEW DLS
> H3: VANCOUVER, CHILLIWACK
> H4: 00100s, 100, SB$. 01
> TILL
> H7: 22000
>
> I Need formatted output something like
>
> Region :
> VANCOUVER
> PAGE 1 OF 1
> DISTRICT:
> CHILLIWACK
> DATE OF SCALE
> Scaled by : FERGUS HAYMAN
>
> SITE SCALER RETURN
> 71G 9085 100
>
> :? Does the f.readline() function automatically put the input
> file into a list ie []
> or a string? I though a list would be right for text
> formatting. and file maniputaltion.
>
> ( I can't figure out how to access say item 71G in H2: and assign
> it a place on a print statement)
>
> This is the header first 7 lines i need to be able to slice and dice the
> log data that follows for species,
> volume etc.
More information about the Python-list
mailing list