首页 > 解决方案 > 如何从文本文件中删除项目或字符串

问题描述

我已经打开了一个文本文件作为 tf 我想从 txt.file 中删除例如文本“Hello” 如何做到这一点是最快的方法?我要删除的文本保存为 "+" , list_of_numbers

import os
from tkinter import *
from tkinter import filedialog
import selenium
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium import webdriver
from selenium import webdriver
import contextlib as textmanager

PATH= "C:\Program Files (x86)\chromedrivers\chromedriver.exe"
driver= webdriver.Chrome(PATH)


list_of_numbers=[]
full_list_of_numbers_work=[]

在这里,我将文件打开为 tf

def openFile():
    tf = filedialog.askopenfilename(
        initialdir="C:/Users/MainFrame/Desktop/",
        title="Open Text file",
        filetypes=(("Text Files", "*.txt"),)
        )
    pathh.insert(END, tf)
    with open(tf) as f:  # use context manager
        for line in f:
            txtarea.insert(END, line)
            line = line.strip()
            if line:
                list_of_numbers.append(int(line))








ws = Tk()
ws.title("PythonGuides")
ws.geometry("400x450")
ws['bg']='#fb0'

txtarea = Text(ws, width=40, height=20)
txtarea.pack(pady=20)

pathh = Entry(ws)
pathh.pack(side=LEFT, expand=True, fill=X, padx=20)



Button(
    ws,
    text="Open File",
    command=openFile
    ).pack(side=RIGHT, expand=True, fill=X, padx=20)


ws.mainloop()


driver.get("https://login.yahoo.com/?.lang=en-US&src=homepage&.done=https%3A%2F%2Fwww.yahoo.com%2F&pspid=2023538075&activity=ybar-signin")

input_field= driver.find_element_by_id("login-username")
button_for_next= driver.find_element_by_id("login-signin")



counting=0

这是我想从文件中删除文本的地方,我想要一种快速的方法

while counting !=len(list_of_numbers):
    input_field.send_keys("+", list_of_numbers[counting])
    button_for_next.click()
    if len(driver.find_elements_by_css_selector("p[data-error]")) > 0:
        #remove the item which is "+" , list_of_numbers[counting]

标签: pythonlistseleniumfileselenium-chromedriver

解决方案


推荐阅读