I am searching some specific strings, however; I find it in places where it shouldn't be. Because I convert pdf files to .txt using html. I found some pattern that I can reach string which i dont want it.
This is the string I want to delete "6\n5\n4\n3\n2\n1\nD\nC\nB\nA" . I am looking " \nC\n" in some other text block not here. So if i find pattern " \nC\nB", I can delete but how I don't figure out. I write inplace_change function but i got error message like this.
"subprocess.CalledProcessError: Command '['java', '-jar', 'C:\Users\Kronos\AppData\Roaming\Python\Python310\site-packages\tabula\tabula-1.0.5-jar-with-dependencies.jar', '--pages', '1', '--guess', '--format', 'JSON', '383026_C.pdf']' returned non-zero exit status 1."
def findWordInText(name):
a = name+".txt"
count =0
with open(a,'r',encoding='utf-8',errors="ignore") as f:
line = f.read()
i = 0
if(r"\nC\n")in line:
String = r"\nC\n"
ch ="B"
if (String+ch) in line:
print('Need to remove')
txt = String+ch
f.close()
inplace_change(a,txt,"removed")
with open(a,'r',encoding='utf-8',errors="ignore") as f:
line = f.read()
if(r"\nC\n") in line:
txt="C"
writeOnExcel(name,txt)
count +=1
def inplace_change(file,old,new):
with open(file) as f:
s = f.read()
if old not in s:
print('"{old_string}" not found in {filename}.'.format(**locals()))
return
with open(file, 'w') as f:
print('Changing "{old_string}" to "{new_string}" in {filename}'.format(**locals()))
s = s.replace(old, new)
f.write(s)