检查python中srt文件中是否存在任何原始数据元素

2024-05-08 00:58:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个值为[('01:02,02:52,03:30,03:31',)]的变量数据,还有一个srt文件,其内容如下:

    23
    00:00:59,025 --> 00:01:06,042
    can read and display documents that

    24
    00:01:02,016 --> 00:01:08,225
    contain<font color="#E5E5E5"> HTML so Firefox Google Internet</font>

    25
    00:01:06,042 --> 00:01:09,851
    <font color="#CCCCCC">Explorer they all do the same thing</font>

    26
    00:01:08,369 --> 00:01:12,630
    <font color="#E5E5E5">they're going</font><font color="#CCCCCC"> 
    to</font><font color="#E5E5E5"> take that</font><font color="#CCCCCC"> 
    document and</font>

现在我想检查我的数据的任何元素,例如:01:02或03:30或任何其他单独的元素是否存在于srt文件中。 例如:01:02出现在编号为00的24以下:01:02,016-->;00:01:08225

这是python文件,但没有结果:

    import mysql.connector
    import numpy as np
    conn= mysql.connector.connect 
    (user="root",password="",host="localhost",database="videojs")

    # prepare a cursor object using cursor() method
    cursor = conn.cursor()

    def read_from_db():

        cursor.execute("select time_arr from info where id ='52'")
        data=cursor.fetchall()
        print(data)
        #Open the file
        fobj = open("one.srt")  
        text = fobj.read().strip().split()

        #Conditions
        while True:
            s = input("Enter a string: ") #Takes input of a string from user
            if s == "": #if no value is entered for the string
                continue
            if s in text: #string in present in the text file
                print("Matched")
                break
            else: #string is absent in the text file
                print("No such string found,try again")
                continue
        fobj.close()


    read_from_db()

请帮帮我。。。。你知道吗


Tags: 文件thetextinfromreadstringcursor
1条回答
网友
1楼 · 发布于 2024-05-08 00:58:06

事情很少。首先,迭代无限循环,没有出口。第二,检查整个单词,而不是一个单词的一部分。以下是我的建议:

...
text = fobj.read().strip().split()  # this will create array of worlds
working = True
while working:
    s = input("Enter a string: ") #Takes input of a string from user
    if s == "": #if no value is entered for the string
        continue
    for t in text: # iterate over all words in file
        if s in t:  # check if input string is part of single word
            print("Matched")
            working = False
            break
    ...

相关问题 更多 >