[Tutor] Appending an extra column in a data file

Oscar Benjamin oscar.j.benjamin at gmail.com
Wed Apr 10 20:47:53 CEST 2013


On 10 April 2013 17:58, Sayan Chatterjee <sayanchatterjee at gmail.com> wrote:
> Thank Andre for your prompt answer.
>
> I'll figure out the plotting issue once the dat files are made. So it's the
> primary concern.
> For an example I am attaching a dat file herewith. The two columns here are
> 2 numpy arrays.I want to add a third column, to be precise, I want to print
> a parameter value on the third column of the file.

The file you attached is a space-delimited text file. If you want to
add a third column with the value '1.0' on every row you can just do:

with open('old.dat') as fin, open('new.dat', 'w') as fout:
    for line in fin:
        fout.write(line[:-1] + ' 1.0\n')


Oscar


More information about the Tutor mailing list