2015-03-18 70 views
1

這是查看帶有高分條目列表的文本文件並將其放入列表中的代碼部分。我需要然後拆分字符串和整數。從文本文件中整數分割字符串

OutList = input("Which class score would you like to view? ").upper() 

if OutList == 'A': 
    List = open("classA.txt").readlines() 
    print (List) 

elif OutList == 'B': 
    List = open("classB.txt").readlines() 
    print (List) 

elif OutList == 'C': 
    List = open("classC.txt").readlines() 
    print (List) 

此代碼當前打印此:

['Bobby 6\n', 'Thomas 4\n'] 

我需要知道如何分離這些和擺脫「\ n」只是有名稱和得分打印出來的。

回答

0

使用rstrip()

這是你修改的程序:

OutList = input("Which class score would you like to view? ").upper() 

if OutList == 'A': 
    List = open("classA.txt").readlines() 
    print (List) 

elif OutList == 'B': 
    List = open("classB.txt").readlines() 
    print (List) 

elif OutList == 'C': 
    List = open("classC.txt").readlines() 

for item in List: 
    print(item.rstrip()) 

輸出:

Which class score would you like to view? A 
['Bobby 6\n', 'Thomas 4\n'] 
Bobby 6 
Thomas 4 

你可以閱讀更多的rstrip功能here

+0

謝謝!完美的作品 – Shakey0198 2015-03-19 21:47:59