loop through each line in a text file
qtrimble
qtrimble at gmail.com
Mon Mar 1 11:15:19 EST 2010
On Feb 26, 6:19 pm, ru... at yahoo.com wrote:
> On Feb 26, 2:21 pm, qtrimble <qtrim... at gmail.com> wrote:
>
>
>
> > On Feb 26, 4:14 pm, OdarR <olivier.da... at 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(r"C:\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(r"C:\z_paul\ims1999.txt", "r")
for line in fileIN:
if line.startswith("ims"):
year = line[3:7]
day = line[7:10]
newfile = file(r"C:\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()
More information about the Python-list
mailing list