Re: loop through each line in a text file

2010-03-01 Thread qtrimble
On Feb 26, 6:19 pm, ru...@yahoo.com wrote:
 On Feb 26, 2:21 pm, qtrimble qtrim...@gmail.com wrote:



  On Feb 26, 4:14 pm, OdarR olivier.da...@gmail.com wrote:
  snip
below is just a sample.  There are well over 500,000 lines that need
processed.

wer1999001
      31.2234      82.2367
      37.9535      82.3456
wer1999002
      31.2234      82.2367
      37.9535      82.3456

   did you try something as a working basis ?

   Olivier

  Yes but it's very simple -

  fileIN = open(rC:\testing.txt, r)

  for line in fileIN:
      year = line[3:7]
      day = line[7:10]
      print year, day

  This is good since i can get the year and day of year into a variable
  but I haven't gotten any further.

 How about something like (untested):

   for line in fileIN:
        if line.startswith (wer):
            year = line[3:7]
            day = line[7:10]
         else:
            print %s-%s %s % (year, day, line.strip())

 You can adjust the details as needed...

Thanks to all of you for your suggestions.

This is what I have now.  It may not be the most efficient or well
written script but it works!  Thanks again!

fileIN = open(rC:\z_paul\ims1999.txt, r)

for line in fileIN:
if line.startswith(ims):
year = line[3:7]
day = line[7:10]
newfile = file(rC:\z_paul\output\ims + year + day + .txt,
wt)
newfile.write(YEAR,DOY,Y_COORD,X_COORD,TYPE\n)
else:
temp = line.replace('  ',',')
temp2 = temp.replace(', ',',')
newfile.write(year + , + day + temp2)
newfile.close()
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop through each line in a text file

2010-02-26 Thread Alf P. Steinbach

* qtrimble:

I'm a python newbie but I do have some basic scripting experience.  I
need to take the line starting with wer and extract the year and day
of year from that string.  I want to be able to add the year and day
of year from the last line having wer* to the lines occurring in
between wer* lines.  Python seems suitable to do this and I'm fairly
certain I can eventually get this to work but I've been hit with a
very short time frame so I'm looking for any generous help.  The data
below is just a sample.  There are well over 500,000 lines that need
processed.

wer1999001
  31.2234  82.2367
  37.9535  82.3456
wer1999002
  31.2234  82.2367
  37.9535  82.3456


example
   line = wer1999001
   line
  'wer1999001'
   line[3:3+4]
  '1999'
   line[7:7+3]
  '001'
   _
/example

Cheers  hth.,

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


Re: loop through each line in a text file

2010-02-26 Thread OdarR
On 26 fév, 22:08, qtrimble qtrim...@gmail.com wrote:
 I'm a python newbie but I do have some basic scripting experience.  I
 need to take the line starting with wer and extract the year and day
 of year from that string.  I want to be able to add the year and day
 of year from the last line having wer* to the lines occurring in
 between wer* lines.  Python seems suitable to do this and I'm fairly
 certain I can eventually get this to work but I've been hit with a
 very short time frame so I'm looking for any generous help.  The data
 below is just a sample.  There are well over 500,000 lines that need
 processed.

 wer1999001
       31.2234      82.2367
       37.9535      82.3456
 wer1999002
       31.2234      82.2367
       37.9535      82.3456

did you try something as a working basis ?

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


Re: loop through each line in a text file

2010-02-26 Thread qtrimble
On Feb 26, 4:14 pm, OdarR olivier.da...@gmail.com wrote:
 On 26 fév, 22:08, qtrimble qtrim...@gmail.com wrote:



  I'm a python newbie but I do have some basic scripting experience.  I
  need to take the line starting with wer and extract the year and day
  of year from that string.  I want to be able to add the year and day
  of year from the last line having wer* to the lines occurring in
  between wer* lines.  Python seems suitable to do this and I'm fairly
  certain I can eventually get this to work but I've been hit with a
  very short time frame so I'm looking for any generous help.  The data
  below is just a sample.  There are well over 500,000 lines that need
  processed.

  wer1999001
        31.2234      82.2367
        37.9535      82.3456
  wer1999002
        31.2234      82.2367
        37.9535      82.3456

 did you try something as a working basis ?

 Olivier

Yes but it's very simple -

fileIN = open(rC:\testing.txt, r)

for line in fileIN:
year = line[3:7]
day = line[7:10]
print year, day

This is good since i can get the year and day of year into a variable
but I haven't gotten any further.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop through each line in a text file

2010-02-26 Thread John Posner

On 2/26/2010 4:21 PM, qtrimble wrote:


fileIN = open(rC:\testing.txt, r)

for line in fileIN:
 year = line[3:7]
 day = line[7:10]
 print year, day

This is good since i can get the year and day of year into a variable
but I haven't gotten any further.


That's an excellent start. Here's another hint ...

There are two kinds of lines in your input file:

* lines that begin with wer
* all other lines

So you need an if-else section to process the lines in the input file:

  if line.startswith(wer):
  # use the contents of *line* to assign values
  # to variables *year* and *day*
  ...
  else:
  # use the current values of *year* and *day*
  # to process the numbers extracted from *line*

Most likely, you'll also need these functions:

* split() -- chop a string into pieces
* int() -- string-to-integer conversion

HTH,
John



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


Re: loop through each line in a text file

2010-02-26 Thread rurpy
On Feb 26, 2:21 pm, qtrimble qtrim...@gmail.com wrote:
 On Feb 26, 4:14 pm, OdarR olivier.da...@gmail.com wrote:
 snip
   below is just a sample.  There are well over 500,000 lines that need
   processed.

   wer1999001
         31.2234      82.2367
         37.9535      82.3456
   wer1999002
         31.2234      82.2367
         37.9535      82.3456

  did you try something as a working basis ?

  Olivier

 Yes but it's very simple -

 fileIN = open(rC:\testing.txt, r)

 for line in fileIN:
     year = line[3:7]
     day = line[7:10]
     print year, day

 This is good since i can get the year and day of year into a variable
 but I haven't gotten any further.

How about something like (untested):

  for line in fileIN:
   if line.startswith (wer):
   year = line[3:7]
   day = line[7:10]
else:
   print %s-%s %s % (year, day, line.strip())

You can adjust the details as needed...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: loop through each line in a text file

2010-02-26 Thread alex goretoy
I smell homework
-- 
http://mail.python.org/mailman/listinfo/python-list