2

I am new to python and I need a help on formatting an array and storing it in a file.

I have an array say

a = [['C', [0.99813803, -0.00263872, -0.00464602]], ['H', [2.0944175, -0.00242373, 0.00417336]], ['H', [0.63238996, 1.03082951, 0.00417296]], ['H', [0.62561232, -0.52974905, 0.88151021]], ['H', [0.64010219, -0.50924801, -0.90858051]]]

Now I want to store it in a file in the following manner

C   0.99813803 -0.00263872 -0.00464602
H   2.0944175 -0.00242373 0.00417336 
H   0.63238996 1.03082951 0.00417296 
H   0.62561232 -0.52974905 0.88151021
H   0.64010219 -0.50924801 -0.90858051

I tried numpy.savetxt but it stores in array format. I know with, regex I can remove all the special characters, but for me the problem lies only in writing line by line from the array.

Can anyone tell me instead of numpy.savetxt what else I can try ?

2 Answers 2

1
a = [['C', [0.99813803, -0.00263872, -0.00464602]], ['H', [2.0944175, -0.00242373, 0.00417336]], ['H', [0.63238996, 1.03082951, 0.00417296]], ['H', [0.62561232, -0.52974905, 0.88151021]], ['H', [0.64010219, -0.50924801, -0.90858051]]]

with open('file.txt', 'w') as f:
for item, values in a:
    line = "{}   {} {} {}\n".format(item, *values)
    f.write(line)

File contents (output):

C   0.99813803 -0.00263872 -0.00464602
H   2.0944175 -0.00242373 0.00417336
H   0.63238996 1.03082951 0.00417296
H   0.62561232 -0.52974905 0.88151021
H   0.64010219 -0.50924801 -0.90858051
Sign up to request clarification or add additional context in comments.

8 Comments

I'd use unpacking in the loop to simplify a bit, and if the set of values is known to be exactly three elements, always, you could also simplify the format line: for item, values in a:, line = {} {} {} {}\n.format(item, *values) (the final write line is unchanged). Also note: The OP put three spaces between the "line header" and the values.
Yeah it's always 3 elements
@ShadowRanger Changed. Thank you! I've learned something today.
@AndrésPérez-AlbelaH : just for learning am asking what if my array looks like this [ [['C', [0.99813803, -0.00263872, -0.00464602]], ['H', [2.0944175, -0.00242373, 0.00417336]], ['H', [0.63238996, 1.03082951, 0.00417296]], ['H', [0.62561232, -0.52974905, 0.88151021]], ['H', [0.64010219, -0.50924801, -0.90858051]]]]
@VinodPrime: That looks like the same list as in the question, but with an extra layer of brackets around it. You could just do for item, values in a[0]: if it's just a single element list of list of lists. If it's not just a single element, and you want to flatten the outer layer, you'd import itertools then do: for item, values in itertools.chain.from_iterable(a): which would flatten one level of nesting.
|
1

It seems like you are trying to write a tsv or csv file where delimiter is tab or space. So why not use the csv module

import csv
from itertools import chain

a = [['C', [0.99813803, -0.00263872, -0.00464602]], ['H', [2.0944175, -0.00242373, 0.00417336]], ['H', [0.63238996, 1.03082951, 0.00417296]], ['H', [0.62561232, -0.52974905, 0.88151021]], ['H', [0.64010219, -0.50924801, -0.90858051]]]
with open('filename', 'w') as f:
    writer = csv.writer(f, delimiter='\t', dialect='excel-tab')
    writer.writerows([list(chain.from_iterable(i)) for i in a])

Then your file content look like this:

C    0.99813803    -0.00263872    -0.00464602
H    2.0944175    -0.00242373    0.00417336
H    0.63238996    1.03082951    0.00417296
H    0.62561232    -0.52974905    0.88151021
H    0.64010219    -0.50924801    -0.90858051

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.