0
votes

I am importing ascii gridded files (.txt) into ArcMap to create a raster image. I am attempting to do as much as possible using Python code....but haven't got too far yet!

I am currently starting the process by appending the .hdr files to the .txt files. First I am manually converting the hdr file to txt, so I can append two txt files. (I would appreciate it if someone could suggest a quicker way to do this please. Can you append hdr to txt?).

I am then appending the two txt files individually using the following script:

filenames = ['hdr.txt', 'yyyymmout.txt'] with open("yyyymm_hdr", 'w') as outfile: for fname in filenames: with open(fname) as infile: for line in infile: outfile.write(line)

This script is working fine for the individual files, but I would like to attempt to do all the files within the script. I have a file for each month from 1900-2010....e.g. 1200+...

Thanks for your help

1

1 Answers

0
votes

First I am manually converting the hdr file to txt, so I can append two txt files.

You want to attach the contents of hdr.txt before each file? You don't need to append them for this, you can do it on the fly.

If hdr.txt is pretty small, just read the whole thing into memory, and write that to each output file before copying from the input file.


This script is working fine for the individual files, but I would like to attempt to do all the files within the script. I have a file for each month from 1900-2010....e.g. 1200+...

So you just need to write some code that can generate the year-month filenames, right?

('{}{}out.txt'.format(y, m) for y in range(1900, 2011) for m in range(1, 13))

Or, if you don't understand compressions:

for y in range(1900, 2011):
    for m in range(1, 13):
        fname = '{}{}out.txt'.format(y, m)
        with open(fname) as infile:
            # etc.

Also, if you just want to copy an entire file to another file, and the files aren't too large to fit in memory, it's a lot simpler to just read() one file and write() to the other.


So, putting it together, I think what you want is:

with open('hdr.txt') as f:
    hdr = f.read()

for y in range(1900, 2011):
    for m in range(1, 13):
        infname = '{}{}in.txt'.format(y, m)
        outfname = '{}{}out.txt'.format(y, m)
        with open(infname) as infile, open(outfname, 'w') as outfile:
            outfile.write(hdr)
            outfile.write(infile.read())