【问题标题】:How to detect if string has changed如何检测字符串是否已更改
【发布时间】:2020-08-29 18:07:51
【问题描述】:

我想检查特定字符串是否已从值更改。 这些值总是随着时间而变化。如果字符串已更改,我只想执行操作。 字符串从文本文件中获取其值。所以我想知道文本文件是否已更改。 我不知道最好的方法是什么。

with open(settings.txt, 'r') as text_file:
        custom_string = text_file.read().split("\n")[1]

if #custom_string has changed
    print("string has changed to", custom_string)

【问题讨论】:

  • 你真的想知道是字符串改变了还是只有文件改变了?
  • 如果您只关心文件是否已更改,那么最快的方法可能就是简单地使用哈希函数。请参阅stackoverflow.com/questions/22058048/hashing-a-file-in-python 在 python 中的散列文件。这将比检查字符串中的字符在计算上更简单。

标签: python


【解决方案1】:
class Watcher:
    """ A simple class, set to watch its variable. """
    def __init__(self, value):
        self.variable = value

    def set_value(self, new_value):
        if self.value != new_value:
            self.pre_change()
            self.variable = new_value
            self.post_change()

    def pre_change(self):
        # do stuff before variable is about to be changed

    def post_change(self):
        # do stuff right after variable has changed

【讨论】:

    【解决方案2】:
    import time
    file = open("PATH TO THE FILE", "r")
    check_list = file.readlines()
    actual_list = []
    def check():
        global check_list, actual_list
        actual_list = file.readlines()
        for i in range(len(check_list)):
            if len(actual_list >= len(check_list):
                if check_list[i] == actual_list[i]:
                    pass
                else:
                    print("The file changed !")
                    check_list = actual_list
                    break
            else:
                if actual_list[i] == check_list[i]:
                    pass
                else:
                    print("The file changed !")
                    check_list = actual_list
                    break
    while True:
        check()
        time.sleep(5)
    

    如果您的文件发生更改,这将每 5 秒检查一次

    【讨论】:

    • 我有一个问题,为什么在检测到变化时不中断。
    • 谢谢@The Machinist,但我收到了这个错误:if check_list[i] == actual_list[i]: IndexError: list index out of range
    • @JorisSchiettecatte 哦,是的,我想我知道为什么会出现这个错误......这是因为actual_list长度低于check_list长度......查看代码,我更新它
    • @The Machinist 感谢更改,但我遇到了同样的错误:` if actual_list[i] == check_list[i]: IndexError: list index out of range`
    • Ew :/ 等等,这很奇怪,我会尝试修复它
    【解决方案3】:

    我找到了解决方案。我编写了一个简单的代码来检查文件是否已更改。

    import time
    
    with open('name_of_.txt', 'r') as playingnow_text_file1:
        data1 = playingnow_text_file1.read().split("\n")[1]
    
    
    def data1read():
    
        with open('name_of_file.txt', 'r') as playingnow_text_file1:
            global data1
            data1 = playingnow_text_file1.read().split("\n")[1]
    
    def data2read():
        with open('name_of_file.txt', 'r') as playingnow_text_file2:
            data2 = playingnow_text_file2.read().split("\n")[1]
        print("activatie loop")
        if data1 != data2:
            print("data1 is not the same as data2 = file has been changed")
            data1read()
    
    while True:
        data2read()
        time.sleep(2) #loop data2read every 2 seconds
    
    

    【讨论】:

      猜你喜欢
      • 2015-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多