Multiple Operation On a Text File

Viewed 24

Good evening

I have a text file that is written by an OCR, this text files has :

- empty lines

- special characters

i try to remove the empty lines and replace the special character é with a normal character e before making the content to a list and then removing \n symbol from the list so i can have a normal list

the problem is that when i run the code the list ELEMENTS is empty and i suspect because im doing this in multiple steps and changing files multiple times

is it possible to be all done in one go from raw ocr file to formated file to list ?

This Is the content of the raw ocr file :

abrégement

afféterie

crémerie

créneler

hébétement

médecin

préparer

qualité

étrange…

réglementation

with open("raw_text_ocr.txt", "r") as f:  
    with open("tst_f_text.txt", "w") as zh:
        for line in f:
            if line.strip() :
               zh.write(line)

x = open("tst_f_text.txt")
s = x.read().replace("é", "e")
x.close()

z = open("z.txt", "w")
z.write(s)
z.close

with open("z.txt") as f:
    unformated_list = [line for line in f]

ELEMENTS = []
for i in unformated_list:
    ELEMENTS.append(i.strip())

print(ELEMENTS)  
0 Answers
Related