Python Forum
Writing values at a desired column in a line of text file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Writing values at a desired column in a line of text file (/thread-4184.html)



Writing values at a desired column in a line of text file - Gupta - Jul-28-2017

I have a array which has 5 values and I want to write it in a text file with 1st value starting from 1st col and second value from 7th col and 3rd value from 10th column of a single line.. is there any way to formate that array and write it in to a file ? Can anyone pls guide me on how to do it..?


RE: Writing values at a desired column in a line of text file - Larz60+ - Jul-28-2017

Id 1st arrange the list in order you wish, then:

The best was is to use json, which is structured text and can be read by many applications (including yours)
and also by ant text editor.

to save, it's simple:
import json

with open(filename, 'w') as jout:
    json.dump(listname, jout)
You may have to install json
to read back in:
import json

with open(filename) as jin:
    listname = json.load(jin)
[/code]


RE: Writing values at a desired column in a line of text file - DeaD_EyE - Jul-28-2017

After the with statement (next line), the 4 white space indentation is missing.

with open(filename, 'w') as jout:
    # code inside the context manager

# code outside the context manager
# the context manager is closing the file automatically when
# leaving the context block
# is also called, when the program gets an KeyboardInterrupt



RE: Writing values at a desired column in a line of text file - Larz60+ - Jul-28-2017

This is a result of the changed editor.
They were there before copy!
code fixed