这是Project Euler Problem 22解决方案的开始。我有他们的文本文件的示例版本,我试图一次生成一个名称的名称。 This是有关字符串方法的文档的链接。我为该任务编写了以下代码:
def name_gen():
with open('C:/python/namessample.txt') as nameFile:
nameGenerator = (i.strip('"').split(sep='","') for i in nameFile)
for name in nameGenerator:
yield type(name), type(nameGenerator), type(nameFile), name
for i in name_gen():
print(i)
这是代码输出:
(<class 'list'>, <class 'generator'>, <class '_io.TextIOWrapper'>, ['DOUGLASS', 'CORDELL', 'OSWALDO', 'ELLSWORTH', 'VIRGILIO', 'TONEY', 'NATHANAEL', 'DEL', 'BENEDICT', 'MOSE', 'JOHNSON', 'ISREAL', 'GARRET', 'FAUSTO', 'ASA', 'ARLEN', 'ZACK', 'WARNER', 'MODESTO', 'FRANCESCO', 'MANUAL', 'GAYLORD', 'GASTON', 'FILIBERTO', 'DEANGELO', 'MICHALE', 'GRANVILLE', 'WES', 'MALIK', 'ZACKARY', 'TUAN', 'ELDRIDGE', 'CRISTOPHER', 'CORTEZ', 'ANTIONE', 'MALCOM', 'LONG', 'KOREY', 'JOSPEH', 'COLTON', 'WAYLON', 'VON', 'HOSEA', 'SHAD', 'SANTO', 'RUDOLF', 'ROLF', 'REY', 'RENALDO', 'MARCELLUS', 'LUCIUS', 'KRISTOFER', 'BOYCE', 'BENTON', 'HAYDEN', 'HARLAND', 'ARNOLDO', 'RUEBEN', 'LEANDRO', 'KRAIG', 'JERRELL', 'JEROMY', 'HOBERT', 'CEDRICK', 'ARLIE', 'WINFORD', 'WALLY', 'LUIGI', 'KENETH', 'JACINTO', 'GRAIG', 'FRANKLYN', 'EDMUNDO', 'SID', 'PORTER', 'LEIF', 'JERAMY', 'BUCK', 'WILLIAN', 'VINCENZO', 'SHON', 'LYNWOOD', 'JERE', 'HAI', 'ELDEN', 'DORSEY', 'DARELL', 'BRODERICK', 'ALONSO'])
nameFile和nameGenerator的类型似乎是正确的,但理想情况下,name一次应该是一个字符串,而不是字符串列表。我猜猜名称是列表的原因是因为.split会返回一个列表,但是我无法想出一种不同的方法来分隔文件中的名称。我还使用.strip删除了引号和尾随引号。
答案 0 :(得分:5)
该文件包含一行,因此无需在文件上循环。只需遍历拆分列表:
def name_gen():
with open('C:/python/namessample.txt') as nameFile:
for name in nameFile.read().split(','):
yield name.strip('"')
或者只返回整个列表;无论如何你已经将它读入记忆中了:
def name_gen():
with open('C:/python/namessample.txt') as nameFile:
return [name.strip('"') for name in nameFile.read().split(',')]
如果你确实有多行,每行包含多个名称,你可以嵌套循环:
def name_gen():
with open('C:/python/namessample.txt') as nameFile:
for line in nameFile:
for name in line.rstrip('\n').split(','):
yield name.strip('"')