Python Forum
Writing values at a desired column in a line of text file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Writing values at a desired column in a line of text file
#1
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..?
Reply
#2
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]
Reply
#3
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
This is a result of the changed editor.
They were there before copy!
code fixed
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 220 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  Help copying a column from a csv to another file with some extras g0nz0uk 3 403 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  Capturing BS4 values into DF and writing to CSV cubangt 18 1,849 Sep-05-2023, 01:57 PM
Last Post: cubangt
  save values permanently in python (perhaps not in a text file)? flash77 8 1,118 Jul-07-2023, 05:44 PM
Last Post: flash77
  File "<string>", line 19, in <module> error is related to what? Frankduc 9 12,389 Mar-09-2023, 07:22 AM
Last Post: LocklearSusan
  Getting last line of each line occurrence in a file tester_V 1 811 Jan-31-2023, 09:29 PM
Last Post: deanhystad
  How to read csv file update matplotlib column chart regularly SamLiu 2 1,015 Jan-21-2023, 11:33 PM
Last Post: SamLiu
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,061 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Json filter is not capturing desired key/element mrapple2020 1 1,072 Nov-24-2022, 09:22 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020