-1

I am writing a file that contains two vectors (x and der):

file = open('derivada.dat','w')
for i in range(n):    
    file.write(str(x[i]) + ' ' + str(der[i]) + '\n')
file.close()

The .dat is like this way:

1.0 3.9999999999999973
0.5000000000000001 2.0
-0.4999999999999998 -1.9999999999999973
-1.0 -4.000000000000007

How can I modify my code in order to the file be as following? With the columns perfect align.

1.0                   3.9999999999999973
0.5000000000000001    2.0
-0.4999999999999998   -1.9999999999999973
-1.0                  -4.000000000000007
Mateus
  • 139
  • 6

1 Answers1

0

You need to use '\t' (tab) in order to have the separation you want.

As an example: file.write('hello' + \t' + 'world' + '\n')

file2 = open('derivada.dat','r')
for lines in file2:
    print(lines)

Out[1]: hello world

To have a "wide" separation you can simply edit \t to '\t\t\t\t\t' .

file.write('hello' + '\t\t\t\t\t' + 'world' + '\n')

It would result in the following:

Out[2]: hello world

Anna Taylor
  • 319
  • 1
  • 9