Reputation: 128
I use a algorithm that is created on an ASCII file, so usually i have to start from scratch to write the commands by hand.
I am thinking of utilizing python's ability to make the process automated. the final ASCII file has multiple lines with every section being a separate command, with a specific set of standard and additional option after that
My question is how can i start writing something like that. I have been using the ability of python to output files from data manipulation but using multiple lines seems new to me.
The objective is for python to automatically write a specific length of words and then having request from the user to input numbers or word to be filled in at specified place.
my file goes something like this, with CAPITAL letters are the standard write commands while with 'CAPITAL' or 'NUMBER' the set of options i want to input.
PROJECT 'name' 'nr'
SET 'maxerr' 'cdcap' 'inhorg' 'hserr' 'NAUTICAL or CARTESIAN' 'pwtail'
MODE NONSTATIONARY 'TWODIMENSIONAL or ONEDIMENSIONAL'
.....(the list goes on)
COMPUTE NONSTAT 'starting date input' 'time interval' 'ending date input'
STOP
the thing i have in mind when i run the script is to be like this
enter name of project =
enter number of project
enter maxerr = (if no maxerr is entered then proceed to the next input, without writing MAXERR in the file)
My objective is to have terminal ask the user to input a value or string which after will be written and incorporated in the final file, with the results being something like this.
PROJECT 'myname' '1'
SET MAXERR 200 INHORG 1 HSERR 0.9 NAUTICAL PWTAIL 2
MODE NONSTATIONARY ONEDIMENSIONAL
........
COMPUTE 20100101.000000 1 HR 20100102.0000
STOP
thank you very much for the help
Upvotes: 1
Views: 7431
Reputation: 15
f1 = open("read.txt", "r")
f11 = f1.readlines()
f = open("write.txt", "a+")
for i in f11:
f.write(str(i))
Upvotes: 0
Reputation: 1216
Writing multiple lines to a file is not particularly different from writing a single line. A line is terminated by a newline character, "\n"
, and everything that follows that character is on the next line.
So:
f = open("outfile.txt", "w")
f.write("First ") # No newline, line continues
f.write("line")
f.write("\n") # Line ends!
f.write("Second line\n") # Newline character in the same write
f.close()
You can also call other functions in between:
f = open("outfile.txt", "w")
f.write("User input: ")
user_input = raw_input("Your input? ")
f.write(user_input)
f.write("\n")
f.close()
This should allow you to at least attempting a solution to your problem. Good luck! :-)
Upvotes: 2