这是我尝试过的
f = open('names.txt')
for people in f:
print('{0} and {1}'.format(people, people))
输出:
john
and john
vlad
and vlad
但是我的预期输出是
john and john
vlad and vlad
这是怎么了?如何解决?
答案 0 :(得分:3)
如何?
people = people.rstrip()
print('{0} and {1}'.format(people, people))
答案 1 :(得分:2)
您可以尝试删除换行符:
f = open('names.txt')
for people in f:
people = people.replace("\n", "")
print('{0} and {1}'.format(people, people))
通过这种方式,文件f中的下一个人应该只换行。