【问题标题】:python print only the names from text filepython只打印文本文件中的名称
【发布时间】:2012-11-19 17:18:11
【问题描述】:

我无法让它只打印名称。
这是我到目前为止得到的
这是malenames.txt的链接:

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()

【问题讨论】:

    标签: python string python-3.x namedtuple


    【解决方案1】:

    应该这样做:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      • 2015-08-12
      • 2023-03-06
      • 2017-04-22
      • 2022-11-10
      相关资源
      最近更新 更多