简体   繁体   中英

How to write and replace elements in a text file?

I am a bit lost with this concept, and we have no examples to show us how to replace or search within a file.

The prompt:

Write a File

You will be provided a file path for input I, a file path for output O, a string S, and a string T.
Read the contents of I, replacing each occurrence of S with T and write the resulting information to file O.
You should replace O if it already exists.

The inputs are unknown, this is the only code we are provided with:

import sys
I= sys.argv[1]
O= sys.argv[2]
S= sys.argv[3]
T= sys.argv[4]

The only examples we have been provided are how to read a file, and how to write a simple text element into a file.

My code so far:

file1 = open(I, 'r')

data = file1.read()

I am truly stuck.

You don't replace within the file, per se -- you read the file contents into a string, replace within the string, and then write the result to the output file.

  • open input file
  • read into string variable
  • close file
  • replace (Python method) all S with T
  • open output file
  • write string variable
  • close file

First you open the input file, and read the content inside it.

Next you create a loop where you check if the current word is the same with T, if it is, you replace it and finally you write the new string to the output file.

I won't give you any code, because that's what what you need, you need to find your self how to do it.

Good luck

Here is your code:

import sys
I= sys.argv[1] 
O= sys.argv[2] 
S= sys.argv[3]
T= sys.argv[4]

with open(I, 'r') as file_in:
    text = file_in.read()
    text = text.replace(S,T)
    with open(O, 'w') as file_out:
        file_out.write(text);

The with construct only makes sure that the file is closed once you're done, so that you don't have to think about it. The rest is straight-forward.

You can write a function to do it for you.

import sys

def elReplacer(I,O,S,T):
    with open(O,"w") as outputFile:
        outputFile.write(open(I,"r").read().replace(S,T))
    outputFile.close()

I= sys.argv[1] 
O= sys.argv[2] 
S= sys.argv[3]
T= sys.argv[4]

elReplacer(I,O,S,T)

This is very late but I might help someone else since I just had this exact problem with my coursework. I made the variable named "inny" for the inputted file and "outy" for the one that will be my output later.

Steps I took were:

  • Open input file

  • Read the input file and store it in a string

  • Close the file

  • Open the file that needs overwritten (make sure you put it on write mode)

  • use re.sub to substitute S for T

  • write that into the file

  • close the file

This is the code I used below. I realize this can be shortened but if you want to display your work to the prof you can go with this.

import re
inny=open(I,'r')
results1 = inny.read()
inny.close()
outy=open(O,'w')
results2 = re.sub (S,T,results1)
outy.write(results2)
outy.close()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM