2012-11-19 37 views
0

我無法打印它只打印名稱。
這是我走到這一步,
這裏是鏈接的malenames.txt:python僅打印文本文件中的名稱

http://www.ics.uci.edu/~kay/malenames.txt

from collections import namedtuple 
FN = namedtuple('FN','name percent people rank') 
FirstN = namedtuple('FirstN','FN') 

def firstname()->str: 
    '''returns a firstname from text file''' 
    filein = open('malenames.txt','r') 
    for str in filein: 
     s = str.split('\t') 
     print (s) 
    FN1 = FN(s) 
    F1 = FirstN(FN1) 
    for name in F1: 
     print(name) 

firstname() 

回答

1

這應該這樣做:

with open("malenames.txt") as f: 
    for line in f: 
     print (line.split()[0]) 
+0

@squiguy感謝,編輯。 –

+0

感謝它現在的作品 –