访问 python 中的文件:额外的空格
Accessing files in python: extra 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+".")
我想让它说:你好*名字*谁是*年龄*岁,谁最喜欢的颜色是*颜色*。相反,由于我在记录信息时必须将信息放在不同的行上(来自不同的 python 程序),所以我在之后放了一个 \n 。所以输出之后有一个输入,如下所示:
Hello, *Name*
who is *age*
years old and who's favourite colour is *colour*.
在 readline 输出上调用 strip:
interview.readline().strip()
我的代码如下所示:
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+".")
我想让它说:你好*名字*谁是*年龄*岁,谁最喜欢的颜色是*颜色*。相反,由于我在记录信息时必须将信息放在不同的行上(来自不同的 python 程序),所以我在之后放了一个 \n 。所以输出之后有一个输入,如下所示:
Hello, *Name*
who is *age*
years old and who's favourite colour is *colour*.
在 readline 输出上调用 strip:
interview.readline().strip()