Hello,
I would like to add a six-line header to a data file for many files in a loop.

However, I need to add the header to a data properly first...

My inclination (as a python novice) is to write the header and append the data to the header. However, this doesn't work. Can someone recommend a more efficient way to go about adding the header? I am not yet familiar with all the functions and functionality of python, there has to be a better way to do it. Do I have to read the data in line by line to simply attach the header?

Here is my error:
Traceback (most recent call last):
File "C:/work/data/makeheader", line 19, in <module>
newout=header.append(data)
AttributeError: 'str' object has no attribute 'append'

I attached the example data file below.

Here is my code:

import os
#set working directory
workDIR = 'C:\\work\\data'
os.chdir(workDIR)
runlist=os.listdir(workDIR)
data=open('justdata2.txt','a')
outfile=open('output.txt','w')
row1="ncols         1422"
row2="nrows         2044"
row3="xllcorner     409924.44886063"
row4="yllcorner     3631074.3284728"
row5="cellsize      500"
row6="NODATA_value  -9999"
n='\n'
header=row1+n+row2+n+row3+n+row4+n+row5+n+row6+n
newout=header.append(data)
print newout
outfile.write(newout)

You'd better write the datafile line by line to the output file. Here is a way to do it

#from __future__ import with_statement  # <--- uncomment this line if your python is 2.5
import os
workDIR = 'C:\\work\\data'

# this is a multiline string
header = """\
ncols         1422
nrows         2044
xllcorner     409924.44886063
yllcorner     3631074.3284728
cellsize      500
NODATA_value  -9999
"""

old_wd = os.getcwd() # <--- store the initial wd
os.chdir(workDIR)
try:
    with open('output.txt','w') as outfile:
        outfile.write(header)
        with open('justdata2.txt','r') as datafile:
            for line in datafile:
                outfile.write(line)
finally:
    os.chdir(old_wd)  # <--- back to the initial wd

The use of the with statement here ensures that the files are closed afterwards, even if an error occurs (it's not very important). The try...finally ensures that we come back to the initial directory afterwards, even if an error occurred. Otherwise, a way to concatenate strings is the + operator, 'string' + 'string' + n + 'etc' . By curiosity, which version of python are you using ?

I'm using verison 2.5.
Thank you, that works great. I think you are magician with many tricks.


You'd better write the datafile line by line to the output file. Here is a way to do it

#from __future__ import with_statement  # <--- uncomment this line if your python is 2.5
import os
workDIR = 'C:\\work\\data'

# this is a multiline string
header = """\
ncols         1422
nrows         2044
xllcorner     409924.44886063
yllcorner     3631074.3284728
cellsize      500
NODATA_value  -9999
"""

old_wd = os.getcwd() # <--- store the initial wd
os.chdir(workDIR)
try:
    with open('output.txt','w') as outfile:
        outfile.write(header)
        with open('justdata2.txt','r') as datafile:
            for line in datafile:
                outfile.write(line)
finally:
    os.chdir(old_wd)  # <--- back to the initial wd

The use of the with statement here ensures that the files are closed afterwards, even if an error occurs (it's not very important). The try...finally ensures that we come back to the initial directory afterwards, even if an error occurred. Otherwise, a way to concatenate strings is the + operator, 'string' + 'string' + n + 'etc' . By curiosity, which version of python are you using ?

Don't hesitate to upgrade to 2.6, it's an even better python with even more opportunities for tricks !

An upgrade is an excellent idea. I'm mostly using python for geospatial analysis.

2.5 is the verison that is distributed with ArcGIS software, a geospatial analysis software that has both a GUI and a scripting interface with python. ArcGIS is made by ESRI, and they have their own module of functions in python for geospatial data analysis. They need to embrace an upgrade to 2.6.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.