簡體   English   中英

未使用 python 解析文本文件的更新

[英]Updates to text file are not being parsed using python

我正在解析不斷更新的文本文件 ('placlog.txt') 中的數據。 當我運行代碼時,一切都會按預期打印,但是如果在代碼運行時對 placlog 文件有任何更新,則不會打印它。

placlog 文件正在由第三方程序更新,我正在使用上述代碼讀取文件並打印任何更新。

格式化后,文本應通過 Telegram API 發送。 這部分也在最初工作。

import urllib.parse
import time
import requests
import os


def post_to_telegram(msg):
    #print(msg)
    base_url = 'https://api.telegram.org/bot&text="{}'.format(msg)
    requests.get(base_url)

def check_url_inMsgList(stringToMatch, msgList):
    for i in msgList:
        if (stringToMatch in i):
            return False
    return True


try:
    f = open("oldFile.txt", "r")
    msgList = f.read().split("\n")
    f.close()
except:
    f = open("oldFile.txt", "w")
    msgList = []
    f.close()
selections = []
urr = ""
name = ""
pie = ""
ourLines = 2400
url_found = 0
name_found = 0
pie_found = 0
while (True):
    file1 = open('placlog.txt', 'r')
    Lines = file1.readlines()
    file1.close()
    while (True):
        # print("-------------------------------")
        if (ourLines == len(Lines)):
            break
        elif (ourLines > len(Lines)):
            ourLines = 0
        else:
            txt = Lines[ourLines].strip()
            tlist = txt.split("&")
            ourLines = ourLines + 1
            for subtxt in tlist:
                if "eventurl=" in subtxt:
                    a = subtxt[9:len(subtxt) - 3]
                    url = "www.awebsite.com/%23" + a.replace("%23", "/")
                    #url = url.replace("%23", "#")
                    for i in range(10):
                        if "F" + str(i) + "/" in url:
                            url = url.split("F" + str(i) + "/")[0] + "F" + str(i) + "/"
                    urr = url
                    url_found = 1
                elif "bit=" in subtxt:
                    name = urllib.parse.unquote(subtxt[4:len(subtxt)])
                    name_found = 1
                elif "pie\":" in subtxt:
                    a = subtxt.split("price")[1]
                    pie = a.split("\"")[2]
                    pie = float(pie)
                    pie = round(pie, 1)
                    pie = str(pie)
                    pie_found = 1
                    selections.append(url + name + pie)
                    msg = (url + " " + name + " " + pie)
                    stringToFind = url + " " + name
                    if (check_url_inMsgList(stringToFind, msgList)):
                        post_to_telegram(msg)
                        msgList.append(msg)
                        print(msg)
                        f = open("oldFile.txt", "a+")
                        f.write(msg + "\n")
                        f.close()
                    time.sleep(0.5)
                elif "minodds=" in subtxt:
                    a = subtxt.split("minodds=")[1]
                    pie = a.split("&")[0]
                    pie = float(pie)
                    rie = round(pie, 1)
                    pie = str(pie)
                    pie_found = 1
                    selections.append(url + name + pie)
                    msg = (url + " " + name + " " + pie)
                    stringToFind = url + " " + name
                    if (check_url_inMsgList(stringToFind, msgList)):
                        post_to_telegram(msg)
                        msgList.append(msg)
                        print(msg)
                        f = open("oldFile.txt", "a+")
                        f.write(msg + "\n")
                        f.close()
                    time.sleep(0.5)
    time.sleep(1)

我建議使用看門狗,看看這是否對你的情況有幫助。 它可以監視文件系統的更改,因此您可以定義一個在 placlog.txt 文件更改/更新時執行的函數。

一個很好的指南可以在這里找到: http : //thepythoncorner.com/dev/how-to-create-a-watchdog-in-python-to-look-for-filesystem-changes/

從該指南中,您可以簡單地更改定義的函數以滿足您的需求,即

def on_modified(event):
    if event.src_path == "path/to/placlog.txt":
        with open('placlog.txt', 'r') as placlog:
            lines = file1.readlines()
        

你能試試這個,看看它是否有幫助? 我仍然推薦文件 i/o 的 with 語句,因為無論如何你總是希望你的文件關閉。

此鏈接也可能很有用,因為它們還監視單個 .txt 文件: Python Watchdog - src_path 不一致

看門狗文檔: https : //pythonhosted.org/watchdog/

注意:由於您澄清了問題,因此刪除了舊答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM