A. Blackmagic
A. Blackmagic

Reputation: 233

List and String formatting difficulties: importing a txt file, adding strings and writing it to a new file

I'm having trouble with list and string formatting, and writing the changes to a new file. What I'm looking for is:

  1. STRINGS BEFORE
  2. imported txt file contents (a list of string values)
  3. STRINGS AFTER

Where both the preceding and following STRINGS are already defined, and everything is written to a new file!

My end goal, is so that when i import a txt file (containing a list) and run the code, it then is printed to a new file with pre-defined strings added before and after the imported txt file's list.

My code right now is as follows:

text_file = open(r"text_file path", "r")
lines = text_file.read().split(',')
lines.insert(0, "String Values Before")
lines.insert("String Values After")
text_file.close()
lines.write("new_file.txt", "w+")

The problem now is that I am inserting to the list, whereas I want the strings to be separate to the list!

I've been able to produce what I want the written file to look like in the console with this code here:

FIRMNAME = "apple"
FILETYPE = "apple"
REPLYFILENAME = "apple"
SECMASTER = "apple"
PROGRAMNAME = "apple"

text_file = open(r"textfile path", "r+")
lines = text_file.readlines().split('\n')

print(("START-OF-FILE \nFIRMNAME= ") + FIRMNAME) 

print(("FILETYPE= ") + FILETYPE) 

print(("REPLYFILENAME= ") + REPLYFILENAME) 

print(("SECMASTER= ") + SECMASTER) 

print(("PROGRAMNAME= ") + PROGRAMNAME) 


print("START-OF-FIELDS")

print("END-OF-FIELDS")

print("START-OF-DATA")
pprint.pprint(lines) 
print("END-OF-DATA")
print("END-OF-FILE")

I just can't figure out how to write this to a new file! Help!

Upvotes: 1

Views: 89

Answers (4)

Ajax1234
Ajax1234

Reputation: 71451

You have an error you originally call the insert method as you must provide an index; however, you can just append, join the resulting list, and write to the file:

text_file = open(r"text_file path", "r")
lines = text_file.read().split(',')
lines.insert(0, "String Values Before")
lines.append("String Values After")
text_file.close()
new_file = open('text_file_path.txt', 'w')
new_file.write(','.join(lines)+'\n')
new_file.close()

Upvotes: 0

Neo
Neo

Reputation: 3786

Use pformat.
pprint

before_values = ["a", "b", "c"]
data = ["1", "2", "3"]
after_values = ["d", "e", "f"]
with open("outfile.txt", "w) as outfile:
    outfile.write("\n".join(before_values)) # Write before values
    outfile.write(pprint.pformat(data))     # Write data list
    outfile.write("\n".join(after_values))  # Write after values

Upvotes: 1

Richard Neumann
Richard Neumann

Reputation: 3361

Your variable lines is of type list, which does not have a method write.
Furthermore insert requires a position, that your second call is lacking.

You'll need to read the file, concat it with the prefix and suffix values accordingly and then write it to the appropriate output file:

with open("text_file_path", "r") as input_file:
    text = input_file.read()

text = '\n'.join(("String Values Before", text, "String Values After"))

with open("new_file.txt", "w+") as output_file:
    output_file.write(text)

Upvotes: 1

Michael Gecht
Michael Gecht

Reputation: 1444

You could solve it this way:

newFile = 'your_new_file.txt'
oldFile = 'your_old_file.txt'

# Open the new text file
with open(newFile, 'w') as new_file:
    # Open the old text file
    with open(oldFile, 'r') as old_file:
        # Write the line before the old content
        new_file.write('Line before old content\n')

        # Write old content
        for line in old_file.readlines():
            new_file.write(line)

        # Write line after old content
        new_file.write('Line after old content')

Upvotes: 2

Related Questions