所以我正在编写一个使用OCR的程序,当我得到一个带换行符的图像并将其转换为字符串,并打印出来时我得到了
hello there,
how are you?
我想要的时候
hello there, how are you?
我该怎么做?
答案 0 :(得分:0)
我建议你使用这个方法:
imgstring.replace("\n", " ")
或者,如果您知道要在哪个单词中删除换行符,则可以使用正则表达式找到替换换行符的位置。
执行此操作的第三种方法:您可以将字符串转换为如下列表:
thisisanexample = "Hello \nSir!"
examplelist = list(thisisanexample)
print(examplelist) #gives ['H', 'e', 'l', 'l', 'o', ' ', '\n', 'S', 'i', 'r', '!']
然后遍历它以查找并替换您不想要的所有'\n'
。
答案 1 :(得分:0)
您可以通过用空格替换背线来完成此操作。如果old_str
是您的原始字符串:
new_str = old_str.replace("\n"," ")
你可以这样做:
new_str = old_str.split("\n").join(" ")
分两步编写这一行将允许您获得一个元素中只有一行的列表。
N.B:如果replace("\n"," ")
不起作用,您可以尝试replace(r"\n", " ")