【发布时间】:2018-05-06 21:30:00
【问题描述】:
我的文件读取的是 teamNames.txt 文件:
Collingwood
Essendon
Hawthorn
Richmond
代码:
file2 = input("Enter the team-names file: ") ## E.g. teamNames.txt
bob = open(file2)
teamname = []
for line1 in bob: ##loop statement until no more line in file
teamname.append(line1)
print(teamname)
输出是:
['Collingwood\n', 'Essendon\n', 'Hawthorn\n', 'Richmond\n']
我想这样做,所以输出将是:
Collingwood, Essendon, Hawthorn, Richmond
【问题讨论】:
-
当您将
line1附加到teamname时,只需在strip之前使用line1使用teamname.append(line1.strip())。在 print 中,使用print(“, “.join(teamname))将您的列表转换为字符串
标签: python string list split append