小编hen*_*nry的帖子

使用Python修复HTML标签括号

我有很多HTML文字,例如

text = 'Hello, how <sub> are </sub> you ? There is a <sub> small error </sub  in this text here and another one <sub> here /sub> .'
Run Code Online (Sandbox Code Playgroud)

有时HTML标签,比如<sub></sub>缺少的<括号内。这会在以后的代码中导致困难。现在,我的问题是:如何才能智能地检测出那些缺失的支架并进行修复?

正确的文本为:

text = 'Hello, how <sub> are </sub> you ? There is a <sub> small error </sub>  in this text here and another one <sub> here </sub> .'
Run Code Online (Sandbox Code Playgroud)

当然,我可以对所有可能的括号配置进行硬编码,但这会花费很长时间,因为我的文字中存在更多类似的错误。

text = re.sub( r'</sub ', r'</sub>', text) 
text = re.sub( r' /sub>', r'</sub>', text)
Run Code Online (Sandbox Code Playgroud)

...并且之前的代码可能会添加另一个括号来更正示例。

python string

5
推荐指数
1
解决办法
81
查看次数

在 Python 中使用滚动条显示大图像

我想使用 tkinter (Python 3) 显示图像。图片很长。因此,我想添加一个垂直滚动条。这是我尝试过的:

(基于这个问题:Scrollbars for a .jpg image on a Tkinter Canvas in Python

import tkinter
import PIL.Image, PIL.ImageTk

# Create a window
window = tkinter.Tk()

frame = tkinter.Frame(window, bd=2) # relief=SUNKEN)

frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)

xscrollbar = tkinter.Scrollbar(frame, orient=tkinter.HORIZONTAL)
xscrollbar.grid(row=1, column=0, sticky=tkinter.E+tkinter.W)

yscrollbar = tkinter.Scrollbar(frame)
yscrollbar.grid(row=0, column=1, sticky=tkinter.N+tkinter.S)

canvas = tkinter.Canvas(frame, bd=0, xscrollcommand=xscrollbar.set, yscrollcommand=yscrollbar.set)
canvas.grid(row=0, column=0, sticky=tkinter.N+tkinter.S+tkinter.E+tkinter.W)
canvas.config(scrollregion=canvas.bbox(tkinter.ALL))

File = "FILEPATH"
img = PIL.ImageTk.PhotoImage(PIL.Image.open(File))
canvas.create_image(0,0,image=img, anchor="nw")

xscrollbar.config(command=canvas.xview)
yscrollbar.config(command=canvas.yview)

frame.pack()
window.mainloop()
Run Code Online (Sandbox Code Playgroud)

我得到以下信息:

我能够绘制图像,但滚动条不起作用。它们只是灰色且不起作用。

在此处输入图片说明

python tkinter

3
推荐指数
1
解决办法
4581
查看次数

识别文本中的句子

我在正确识别文本中特定极端情况的句子时遇到了一些麻烦:

  1. 如果涉及到点、点、点,则不保留。
  2. 如果"涉及的话。
  3. 如果句子不小心以小写开头。

到目前为止,这就是我识别文本中句子的方法(来源:字幕重新格式化以完整句子结尾):

re.findall部分基本上查找str以大写字母 开头的块,[A-Z]然后是除标点符号之外的任何内容,然后以标点符号结尾[\.?!]

import re
text = "We were able to respond to the first research question. Next, we also determined the size of the population."
    for sentence in re.findall(r'([A-Z][^\.!?]*[\.!?])', text):
        print(sentence + "\n")
Run Code Online (Sandbox Code Playgroud)
We were able to respond to the first research question.

Next, we also determined the size of the population.
Run Code Online (Sandbox Code Playgroud)

极端情况 1:点、点、点

点,点,点不会被保留,因为没有给出如果三个点连续出现该怎么办的说明。这怎么能改变呢?

text = "We were able to respond to …
Run Code Online (Sandbox Code Playgroud)

python regex string python-3.x

3
推荐指数
1
解决办法
4441
查看次数

杀死所有锁定文件的进程

当我运行 Python 脚本时,有时会收到以下错误:

“该进程无法访问该文件,因为它正在被另一个进程使用”

现在我想知道: python 有没有办法:

  1. 检测哪个进程正在使用该文件?
  2. 关闭这个进程?(例如使用os.system('taskkill /f /im PROCESS.exe)

python windows process

3
推荐指数
1
解决办法
2440
查看次数

分辨率提高后 np.gradient 的奇怪行为

我想在同一个图上绘制 cos(x) 及其导数 -sin(x) 。我做了以下事情:

import numpy as np    
x = np.linspace(0,10,20) #x values: 0-10, 20 values
f = np.cos(x) # function
df = np.gradient(f) # derivative

# plot
plt.plot(f, label ="function")
plt.plot(df, label ="derivative")
plt.legend()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

在这里您已经可以看到导数的幅度存在问题。应该是1,但是大约是0.5。

如果我现在将分辨率从 20 点增加到 50 点,导数的幅度会下降得更多:

在此输入图像描述

100分:

在此输入图像描述

1000分:

在此输入图像描述

有谁知道发生了什么事吗?

python numpy

3
推荐指数
1
解决办法
779
查看次数

多进程视频处理

我想对相邻帧进行视频处理。更具体地说,我想计算相邻帧之间的均方误差:

mean_squared_error(prev_frame,frame)
Run Code Online (Sandbox Code Playgroud)

我知道如何以线性直接的方式计算它:我使用 imutils包利用队列来解耦加载帧和处理它们。通过将它们存储在队列中,我不需要在处理它们之前等待它们。......但我想更快......

# import the necessary packages to read the video
import imutils
from imutils.video import FileVideoStream
# package to compute mean squared errror
from skimage.metrics import mean_squared_error

if __name__ == '__main__':

    # SPECIFY PATH TO VIDEO FILE
    file = "VIDEO_PATH.mp4" 

    # START IMUTILS VIDEO STREAM
    print("[INFO] starting video file thread...")
    fvs = FileVideoStream(path_video, transform=transform_image).start()

    # INITALIZE LIST to store the results
    mean_square_error_list = []

    # READ PREVIOUS FRAME
    prev_frame = fvs.read()

    # LOOP over frames …
Run Code Online (Sandbox Code Playgroud)

python opencv multiprocessing imutils

2
推荐指数
1
解决办法
794
查看次数

零数组减 1 给出 255 数组?

我有这个代码:

a = np.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype='uint8')
a-1
Run Code Online (Sandbox Code Playgroud)

为什么我得到:

array([255, 255, 255, 255, 255, 255, 255, 255, 255, 255], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

其结果?

并不是:

array([-1, -1, -1,...], dtype=uint8)
Run Code Online (Sandbox Code Playgroud)

python numpy

0
推荐指数
1
解决办法
98
查看次数