访问python中的文件:额外的空格

时间:2016-02-11 18:32:00

标签: python file spaces

我的代码如下所示:

fileName = "interview.txt"
accessMode = "r"
interview = open(fileName, accessMode)

name = interview.readline()
age = interview.readline()
colour = interview.readline()
print("Hello, "+name+" who is "+age+" years old and who's favourite colour is "+colour+".")

我希望它说:Hello * name *谁是*年龄*岁,谁最喜欢的颜色是* color *。相反,因为我必须在记录信息时将信息放在不同的行上(来自不同的python程序),然后我放了一个\ n。所以输出后面有一个输入:

 Hello, *Name*
 who is *age*
 years old and who's favourite colour is *colour*.

1 个答案:

答案 0 :(得分:3)

读取线输出上的调用条:

interview.readline().strip()