变量的值不等于此代码中的值

时间:2018-01-09 09:02:15

标签: python parsing variables operators analysis

在下面的代码中,我有一个变量“split_data [i]”,我将此变量的每个元素与“text variable:

中的文件进行比较
    i = 0
    while i < len(split_data):
        print(split_data[i])
        if  split_data[i] in text: #string in present in the text file
            print("Matched" )
        else:
            print("not matched" )    
        i += 1

split_data [4] 的值是 '03:31',它出现在文本中,但仍然输出为“not”匹配” 如果我的代码是这样的:

    i = 0
    while i < len(split_data):
        print(split_data[i])
        if  '03:31' in text: #string in present in the text file
            print("Matched" )
        else:
            print("not matched" )    
        i += 1

输出匹配。 或者我们可以说:

    if '03:31' in split_data[4]:
        print("true")
    else:
        print('false')

是真的但是

    if '03:31' == split_data[4]:
        print("true")
    else:
        print('false')

是假的

这是我的全部代码:

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

def read_from_db():

    cursor = conn.cursor()
    cursor.execute("select time_arr from info where id ='53'")
    data=cursor.fetchone()
    print(data)

    the_new_list = [x.split(',') for x in data]
    print(the_new_list)
    str_data = ''.join(map(str, the_new_list))
    print(len(str_data))
    split_data=str_data.strip('[]').split(',')
    print(split_data)
    print(len(split_data))  

    i = 0
    while i 

please guide me how to resolve this i am a beginner

this is the data in text:

    1
    00:00 --> 00:06
    welcome to your very<font color="#CCCCCC"> first tutorial</font><font 
    color="#E5E5E5"> on</font>

    2
    00:02 --> 00:08
    beginning HTML HTML<font color="#E5E5E5"> is the bedrock</font><font 
    color="#CCCCCC"> of</font>

    3
    00:06 --> 00:10
    <font color="#CCCCCC">the world wide web</font><font color="#E5E5E5"> 
     and if you're going</font>

     4  
     00:08 --> 03:31
    <font color="#CCCCCC">to do anything</font><font color="#E5E5E5"> 
    even</font><font color="#CCCCCC"> in</font><font color="#E5E5E5"> other 
    languages</font>

1 个答案:

答案 0 :(得分:0)

也许您可能想要重新访问循环结构。
考虑一下片段:

text        = "one two three four five"
split_data  = ["one", "two", "three", "four", "five"]

for i in split_data:
    print( "matched" if i in text else "not matched" )