问题
I'm working in a project with OCR in Spanish. The camera captures different frames in a line of text. The line of text contains this:
Este texto, es una prueba del dispositivo lector para no videntes.
After some operations I get strings like that:
s1 = "Este texto, es una p!"
s2 = "fste texto, es una |prueba u.-"
s3 = "jo, es una prueba del dispo‘"
s4 = "prueba del dispositivo \ec"
s5 = "del dispositivo lector par:"
s6 = "positivo lector para no xndev"
s7 = "lector para no videntes"
s8 = "¡r para no videntes."
I would like to join the string so that I can get the text of the scanned line in a final string like that:
sf = "Este texto, es una prueba del dispositivo lector para no videntes."
To begin I tried to use SequenceMatcher
between two strings but it was not effective:
# -*- coding: utf-8 -*-
from difflib import SequenceMatcher as sq
s1 = "Este texto, es una p!"
s2 = "fste texto, es una prueba u.-"
match = sq(None, s1, s2).find_longest_match(0, len(s1), 0, len(s2))
print unicode(s1 + s2[match.b + match.size:])
The result has invalid characters like |
or !
:
>>>Este texto, es una p!|prueba u.-
Between s2
and s3
:
>>>fste texto, es una |prueba u.-prueba del dispo‘
Etc. I'm using python 2.7 on Windows 7.
回答1:
You should use regex Do something like
import re
line = re.sub(r'\W', r'', line)
\W means any none word character. You may read more about regexes at site: https://docs.python.org/2/library/re.html
来源:https://stackoverflow.com/questions/41624787/how-to-delete-invalid-characters-between-multiple-strings-in-python