Python 使用spyder中的Pytesharact打印到控制台时出现问题

Python 使用spyder中的Pytesharact打印到控制台时出现问题,python,anaconda,spyder,tesseract,python-tesseract,Python,Anaconda,Spyder,Tesseract,Python Tesseract,我目前正在windows 10上通过anaconda与python 3.8.5一起使用spyder,运行以下代码时: import cv2 import pytesseract pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe" img_path ='img/gotta-go-fast.jpg' img = cv2.imread(img_pat

我目前正在windows 10上通过anaconda与python 3.8.5一起使用spyder,运行以下代码时:

import cv2
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\\Program Files\\Tesseract-OCR\\tesseract.exe"

img_path ='img/gotta-go-fast.jpg'

img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

result = pytesseract.image_to_string(img, lang='eng')
print(result)
spyder中的IPython控制台将自动清除,但如果我只写入以下文本文档,则不会发生同样的情况:

result = pytesseract.image_to_string(img, lang='eng')

with open('text_result.txt', mode ='w') as file:
    file.write(result)
result = pytesseract.image_to_string(img, lang='eng')
arr = result.split('\n')[0:-1]
result = '\n'.join(arr)

是否有办法解决此问题?

找到了发生的情况。当pytessaract读取文本时,在最后一次读取行之后的新行中添加了一个
\x0c
命令。 通过删除以下命令解决了此问题:

result = pytesseract.image_to_string(img, lang='eng')

with open('text_result.txt', mode ='w') as file:
    file.write(result)
result = pytesseract.image_to_string(img, lang='eng')
arr = result.split('\n')[0:-1]
result = '\n'.join(arr)