Python 3.x 查找文本文件中的项,该文本文件是以python中的某个大写字母开头的大写单词的咒语字符串

Python 3.x 查找文本文件中的项,该文本文件是以python中的某个大写字母开头的大写单词的咒语字符串,python-3.x,Python 3.x,我正在尝试提取保存到文本文件中的输入名称字符串。我需要通过输入的大写字母来提取它们。即,保存的文本文件包含名称DanielDanClark,我需要提取以D开头的名称。我被卡在这部分 for i in range(num): print("Name",i+1," >> Enter the name:") n=input("") names+=n file=open("names.txt","w") file.write(names) loo

我正在尝试提取保存到文本文件中的输入名称字符串。我需要通过输入的大写字母来提取它们。即,保存的文本文件包含名称DanielDanClark,我需要提取以D开头的名称。我被卡在这部分

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()

我知道最后4行可能是错误的。这是我在上次失败的尝试中尝试的

让我们一块一块地分解代码

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
num = 5
names = ""
for i in range(num)
    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n
我冒昧地给
num
一个值
5
,给
names
一个值
,这样代码就可以运行了。这个街区没有问题。并将创建一个名为
names
字符串,其中包含所有输入。您可以考虑在其中插入分隔符,这使得读取数据更加容易。建议使用
\n
这是一种换行符,因此当您开始编写文件时,每行实际上有一个名称,例如:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
num = 5
names = ""
for i in range(num)
    print("Name",i+1," >> Enter the name:")
    n = input()
    names += n + "\n"  
file = open(r"c:\somedir\somesubdir\names.txt","w")
file.write(names)
file.close()
现在您将要编写文件:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
file=open("names.txt","w")
file.write(names)
在此块中,您忘记关闭文件,更好的方法是完全指定文件的路径名,例如:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
num = 5
names = ""
for i in range(num)
    print("Name",i+1," >> Enter the name:")
    n = input()
    names += n + "\n"  
file = open(r"c:\somedir\somesubdir\names.txt","w")
file.write(names)
file.close()
或者更好地使用

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
with open(r"c:\somedir\somesubdir\names.txt","w") as openfile:
    openfile.write(names)
下面是询问用户是否要查找名称然后退出的块:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
首先,您使用的是生产代码中不应使用的
quit()
,请参阅您真正应该使用的
sys.exit()
的答案,这意味着您需要导入
sys
模块。然后继续获取答案的数值为
N
N
,并在
if
语句中进行检查。您不必执行
ord()
您可以在
if
语句中直接使用字符串比较。例如:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
lookup = input("Did you want to look up any names?(Y/N)")
if lookup.lower() == "n":
    sys.exit()
然后,在前面的
if
语句的
else:
块中继续查找请求的数据:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
这也不能正常工作,因此分隔符
\n
就在这里派上了用场。打开文本文件时,可以使用
for line in file
块逐行枚举文件,并在第一个块中添加
\n
分隔符,每一行都是一个名称。您在
for letter in file
块中也会出错,它没有执行您认为应该执行的操作。它实际上返回文件中的每个字母,而不管您在前面的
输入中键入什么。以下是一个工作示例:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
letter = input("Enter the first letter of the names you want to look up in uppercase:")
result = []
with open(r"c:\somedir\somesubdir\names.txt", "r") as openfile:
    for line in openfile:  ## loop thru the file line by line
        line = line.strip('\n')  ## get rid of the delimiter
        if line[0].lower() == letter.lower():  ## compare the first (zero) character of the line 
            result.append(line)  ## append to result
print(result)  ## do something with the result
总而言之:

for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()
import sys

num = 5
names = ""
for i in range(num)
    print("Name",i+1," >> Enter the name:")
    n = input("")
    names += n + "\n"          

with open(r"c:\somedir\somesubdir\names.txt","w") as openfile:
    openfile.write(names)

lookup = input("Did you want to look up any names?(Y/N)")
if lookup.lower() == "n":
    sys.exit()

letter = input("Enter the first letter of the names you want to look up in uppercase:")
result = []
with open(r"c:\somedir\somesubdir\names.txt", "r") as openfile:
    for line in openfile:  
        line = line.strip('\n') 
        if line[0].lower() == letter.lower():  
            result.append(line) 
print(result)  

我想指出的一个警告是,当您创建文件时,您将在
w
模式下打开该文件,这将每次创建一个新文件,从而覆盖以前的文件。如果要附加到文件,需要在
a
模式下打开它,该模式将附加到现有文件,或者在文件不存在时创建新文件。

谢谢。我知道我必须用另一种方式来做。我一直想让它这样工作,因为我被告知它会工作。谢谢你的答复
for i in range(num):

    print("Name",i+1," >> Enter the name:")
    n=input("")
    names+=n          
file=open("names.txt","w")
file.write(names)
lookUp=input("Did you want to look up any names?(Y/N)")
x= ord(lookUp)
if x == 110 or x == 78:
    quit()
else:
    letter=input("Enter the first letter of the names you want to look up in uppercase:")
    file=open("names.txt","r")
    fileNames=[]
    file.list()
    for letter in file:
        fileNames.index(letter)
    fileNames.close()