在Python中读取列表

在Python中读取列表,python,list,Python,List,我想访问与存储在文本文件(stores.txt)中的字母表的每个字母相关联的分数。每一行都是一个索引,我只想检索每个字母的数字,并在将某个字母输入函数时打印出来。我将如何分割项目,以便获得号码并返回它 Scores.txt is shown like this: A 1 B 3 C 5 D 3 E 1 F 5 G 4 H 3 I 1 J 10 K 8 L 3 M 5 N 3 O 2 P 5 Q 20 R 3 S 3 T 2 U 1 V 10 W 12 X 16 Y 8 Z 20 This

我想访问与存储在文本文件(stores.txt)中的字母表的每个字母相关联的分数。每一行都是一个索引,我只想检索每个字母的数字,并在将某个字母输入函数时打印出来。我将如何分割项目,以便获得号码并返回它

Scores.txt is shown like this: 
A 1
B 3
C 5
D 3
E 1
F 5
G 4
H 3 
I 1
J 10
K 8
L 3
M 5
N 3
O 2
P 5
Q 20
R 3
S 3
T 2
U 1
V 10
W 12
X 16
Y 8
Z 20

This is the code ive used up to now, however this would result in displaying the whole line 
(['A','1']) when i passed A to the function and I would like just 1 to be passed.

def getLetterScore(user_Input):
scoreFileT1 = open('D:/DELL/Desktop/Programming Assignment/scores.txt')
score = []
for line in scoreFileT1:
        line = line.strip(' ')
        line = line.split()
        score.append(line)
scoreFileT1.close()
if user_Input =='a' or user_Input == 'A':
    print(score[0])

这是我想出的代码。 在这种情况下,如果您输入任何字母,将打印该字母的相应数字

def getLetterScore(user_Input):
    scoreFileT1 = open('C://Users//Naveendran//Desktop//scores.txt')
    score = []
    for line in scoreFileT1:
        line = line.strip(' ')
        line = line.split()
        score.append(line)
    scoreFileT1.close()
    for i in range(len(score)):
        if user_Input.upper() == score[i][0]: 
            print(score[i][1])
        

b = input("Enter a letter:")
getLetterScore(b)

我建议你用这个密码

rfile=open("score.txt")   #add your path here
file=rfile.read()
filesplit=file.split("\n")
letter=input("enter The number").capitalize()
for i in filesplit:
    if(i[0]==letter):
        for j in range(2,len(i)):
            print(i[j],end="")

请遵守StackOverflow指南。本网站不是为你的编程任务寻找答案的!因为这似乎是一个家庭作业问题,所以我只会给你们指点,而不是直接回答。希望这从长远来看对您更有帮助:1)line.split()操作创建一个元组。问问你自己:如何打开元组?或者:如何访问元组的特定元素2)您使用了一个列表作为分数变量。我建议改为听写。问问自己:为什么这是更好的选择?3) 通常使用“with”而不是open()…close()打开文件附加说明:4)通过使用upper()或lower()方法简化有关“a”和“a”检查的代码。