1

I have a input text file as follows, this is saved as 12.txt:

[(442, 165), (442, 184), (487, 165), (487, 184)],english

My aim is to remove all the special characters from this file and overwrite it : I am using below python script :

import os
import numpy as np
import math
import cv2 as cv

#path = '/media/D/code/OCR/text-detection-ctpn/data/mlt_english+chinese/image'
gt_file = '12.txt'

with open(gt_file, 'r+') as f:
    for line in f.readlines():
        line = line.replace("[", "")
        line = line.replace("(", "")
        line = line.replace(")", "")
        line = line.replace("]", "")
        line = line.replace(" ", "")

        f.write(line)

However it gives me this output:

[(234, 162), (234, 183), (307, 162), (307, 183)],english
234,162,234,183,307,162,307,183,english

I dont want the output to be appended as it is as shows above I want the output to overwrite 12.txt. Filethat is 12.txt after running python script should look like this:

234,162,234,183,307,162,307,183,english

I have referred Python replace and overwrite instead of appending but there is some thing I am missing

Ajinkya
  • 1,797
  • 3
  • 24
  • 54

4 Answers4

2

You need to open temp file and read from file, remove old file and rename to new name

import os
import numpy as np
import math
import cv2 as cv

#path = '/media/D/code/OCR/text-detection-ctpn/data/mlt_english+chinese/image'
gt_file = '12.txt'
output = open("temp.txt","w")
with open(gt_file, 'r') as f:
    for line in f:
        line = line.replace("[", "")
        line = line.replace("(", "")
        line = line.replace(")", "")
        line = line.replace("]", "")
        line = line.replace(" ", "")

        output.write(line)
output.close()
os.remove(gt_file) # remove old file
os.rename("temp.txt",gt_file) # rename as old file
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
Marek Maszay
  • 1,537
  • 1
  • 9
  • 11
2

Separate the two processes, it's way cleaner.

#read and edit lines
to_write = [] #store edited lines
with open(gt_file, 'r') as f:
    for line in f.readlines():
        ...
        to_write.append(line)

#write back edited lines
with open(gt_file, 'w') as f:
    for line in to_write:
        f.write(line)
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
0

The reason is, you file pointer has reached the end. Given a single line in text file, seek() and truncate() functions can be used to reproduce the results that you require.

import os
import numpy as np
import math

gt_file = '12.txt'

with open(gt_file, 'r+') as f:
    for line in f.readlines():
        line = line.replace("[", "")
        line = line.replace("(", "")
        line = line.replace(")", "")
        line = line.replace("]", "")
        line = line.replace(" ", "")
        f.seek(0)
        f.truncate()
        f.write(line)
Ashwin Geet D'Sa
  • 6,346
  • 2
  • 31
  • 59
0

I used this

file = open('test_file', 'r')
data = file.read()
file.close()

data = data.replace("[", "")
data = data.replace("(", "")
data = data.replace(")", "")
data = data.replace("]", "")
data = data.replace(" ", "")
print(data, file=open('test', 'w'))

Check if this works for you.

Nitin
  • 246
  • 1
  • 7