Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 搜索文本文件的特定行,但有令人讨厌的扭曲_Python_Search_Python 3.6 - Fatal编程技术网

Python 搜索文本文件的特定行,但有令人讨厌的扭曲

Python 搜索文本文件的特定行,但有令人讨厌的扭曲,python,search,python-3.6,Python,Search,Python 3.6,因此,我的python文件需要能够删除文本文件的行。简单,对吗?本网站上有大量关于此主题的已回答问题,不幸的是,这些问题不包括两行字符串中是否有相同的字符序列,例如: datafile.txt: 如果我想删除第二行,我必须搜索它,我的系统工作方式意味着你只知道冒号前的第一部分。例如: LMNO: ABCD: BABC: 因此,如果要删除包含ABCD甚至ABCD:的行,它将返回第1行 这是我当前的代码: import fileinput, sys def searchFiles(searchPr

因此,我的python文件需要能够删除文本文件的行。简单,对吗?本网站上有大量关于此主题的已回答问题,不幸的是,这些问题不包括两行字符串中是否有相同的字符序列,例如: datafile.txt:

如果我想删除第二行,我必须搜索它,我的系统工作方式意味着你只知道冒号前的第一部分。例如:

LMNO:
ABCD:
BABC:
因此,如果要删除包含ABCD甚至ABCD:的行,它将返回第1行

这是我当前的代码:

import fileinput, sys
def searchFiles(searchPrompt):
    with open("testfile.txt") as f:
        for num, line in enumerate(f, 1):
            if searchPrompt in line:
                return(num)

def deleteLine(lineNumber):
    for lineNumber, line in enumerate(fileinput.input('testfile.txt', 
    inplace=1)):
        if lineNumber == 0:
            continue
        else:
            sys.stdout.write(line)   

searchPrompt = input("Enter username to delete: ")
searchPrompt = (searchPrompt+":")
print(searchPrompt)
lineNumber = searchFiles(searchPrompt)
deleteLine(lineNumber)

我想这就是你想要的:

import fileinput, sys

def removeFromFile(file, user):
    pattern = user+":"
    f = open(file, "r+")
    lines = f.readlines()
    f.seek(0)
    for line in lines:
        if not line.startswith(pattern):
            f.write(line)
    f.truncate()
    f.close()

username = input("Enter username to delete: ")
removeFromFile("datafile.txt", username)

您可以在python中使用分隔符为“:”的切片

例如:


我不知道扭曲是什么…你真正的问题是什么-不清楚你想做什么…可能我不明白,但如何:如果line.startswithsearchprompt我认为扭曲是他想从行的开始搜索,而不是在行的任何地方找到给定的字符串。@turtlesallwaydown-是的,确切地
import fileinput, sys

def removeFromFile(file, user):
    pattern = user+":"
    f = open(file, "r+")
    lines = f.readlines()
    f.seek(0)
    for line in lines:
        if not line.startswith(pattern):
            f.write(line)
    f.truncate()
    f.close()

username = input("Enter username to delete: ")
removeFromFile("datafile.txt", username)
def searchFiles(searchPrompt):
    with open("testfile.txt") as f:
        for num, line in enumerate(f, 1):
            if searchPrompt == line.split(":")[0]:
                return(num)

def deleteLine(lineNumber):
    for lineNumber, line in enumerate(fileinput.input('testfile.txt', 
inplace=1)):
        if lineNumber == 0:
            continue
        else:
            sys.stdout.write(line)   

searchPrompt = input("Enter username to delete: ")
searchPrompt = searchPrompt.split(":")[0]
print(searchPrompt)
lineNumber = searchFiles(searchPrompt)
deleteLine(lineNumber)