0

How to Overwite a line in file using python?

this is my list in text file
"Sample" , "S"
"Simple" , "T"
"test" , "S"

how to Overwite the second line?
"Simple", "T" 
into 
"Simple", "S"

then the text file will be change like this:
"Sample" , "S"
"Simple" , "S"
"test" , "S"

Here is my code i use function for the flow

list = []
#put your sample csv file here
fileName = 'list.txt'

#reading file text
def openFile(filename):
   content = open(filename) 
   lines = content.readlines()
   for line in lines: 
      list.append(line)
   return content

def mark(fileName):
   flag = open(pick, "w")
   choice = input("Enter number to mark: ")
   for choice in list:
      flag.write(choice[1].replace('"T"', '"S"')

AnyOne can help me to solve this hard problem??

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Newbieee
  • 167
  • 10

2 Answers2

1

You can try this:

list = []
#put your sample csv file here
fileName = 'list.txt'

#reading file text
def openFile(filename):
    content = open(filename)
    lines = content.readlines()
    for line in lines:
        list.append(line)
    content.close()
    return content

def mark(fileName):
    flag = open(fileName, "w")
    choice = input("Enter number to mark: ")
    list[int(choice)] = list[int(choice)].replace('"T"','"S"')
    for line in list:
        flag.write(line)

cnt = openFile(fileName)
mark(fileName)

Input file (list.txt):

"Sample" , "S"
"Simple" , "T"
"test" , "S"

Output file (out.txt):

"Sample" , "S"
"Simple" , "S"   <---- this value changed from a T to a S
"test" , "S"
Donald S
  • 1,583
  • 1
  • 10
  • 26
  • the out put is still the same ,because only the first column in row to replace not the second column – Newbieee Jun 08 '20 at 06:58
  • Did you want the value in the 2nd column to change? My input and output match your original specification. – Donald S Jun 08 '20 at 07:24
  • i mean the input file can only be change and not to make another out file, only just the list.txt to be change – Newbieee Jun 08 '20 at 07:29
  • is it possible?? – Newbieee Jun 08 '20 at 07:29
  • ok, can you show me what your output looks like after you run my code? BTW, the file name is different, if you want the same name, you can add a rename "remove/move" command – Donald S Jun 08 '20 at 07:30
  • the list.text not change – Newbieee Jun 08 '20 at 07:34
  • this is the list.txt "Refinery Coffee & Tea", "N" "Hummus Elijah", "N" "Manam", "N" "Tittos Latin BBW", "N" "Shinsen Sushi Bar", "N" "Blake's Wings & Steaks", "N" "Kanto Freestyle Breakfast", "Y" "The Giving Nafe", "N" "el Chupacabra", "Y" "Ebi 10", "N" "Jumong", "Y" – Newbieee Jun 08 '20 at 07:35
  • and the out.txt "Refinery Coffee & Tea", "Y" "Hummus Elijah", "N" "Manam", "N" "Tittos Latin BBW", "N" "Shinsen Sushi Bar", "N" "Blake's Wings & Steaks", "N" "Kanto Freestyle Breakfast", "Y" "The Giving Nafe", "N" "el Chupacabra", "Y" "Ebi 10", "N" "Jumong", "Y" – Newbieee Jun 08 '20 at 07:35
  • how can i remove and rename file?? – Newbieee Jun 08 '20 at 07:37
  • Hi, yes it is possible. I added 4 lines of code to the original to take care of this. I noticed you don't have "S" or "T" in this example, now you have "N" and "Y", correct? Keep in mind that replace will replace all matches by default. If you want a specific one replaced, will need to do more work – Donald S Jun 08 '20 at 07:39
  • in the instructions on my project , "Do not use any libraries" – Newbieee Jun 08 '20 at 07:47
  • easy fix for that. just use the same file name, list.txt, in the write statement. I modified the code above, should do what you want now. – Donald S Jun 08 '20 at 08:04
1

I think this should be enough for your purposes.

with open(filename, 'r+') as f:
    content = f.read().replace('"T"', '"S"')
    f.seek(0)
    f.write(content)
    f.truncate()
revliscano
  • 2,227
  • 2
  • 12
  • 21