Stuck

sblakey at freei.com sblakey at freei.com
Tue May 16 16:54:28 EDT 2000


-----On Wed May 17 13:14:05 2000 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:]
>
I think this does what you tried to do with the above code:

import string
filename = raw_input('Enter file : ')
file = open(filename)
line = file.readline()
while line:
	if 0 == string.find(line, 'H3'):	# Does the line start
						# with H3?
		region, district = string.split(line)[-2]
		while ',' == region[-1]:
			region = region[:-1]	# Strip off ','
		print ' Region\t:', region
		print ' District\t:', district
	line = file.readline()

Note that you can't just read from the filename given, you need to call
open() to open the file.
readline() returns one string.  string.split() will split this into
seperate words.
>
>
>
>
>
>        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?

A string.  You will need to manipulate this string with the functions in
the string library to get what you want.

>          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)
>
if 0 == string.find(line, 'H2'):
	site = string.split(line)[1]
	print 'Site\t:', site
>
>
>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.
>
>-- 
>http://www.python.org/mailman/listinfo/python-list
>





More information about the Python-list mailing list