在python中访问文件:额外的空间
我的代码如下所示:在python中访问文件:额外的空间,python,file,spaces,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 col
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。因此,输出有一个enter,如下所示:
Hello, *Name*
who is *age*
years old and who's favourite colour is *colour*.
读取线输出上的调用条:
interview.readline().strip()
最简单:
interview.readline().strip()
最好使用rstrip
@zondo我以前也这么想,直到我意识到当我收到意外输入时,我一直在回去更改它们。现在我假设如果你使用了其中一个方向变量,你真的是出于某种原因,所以这是我唯一一次再使用它。什么时候你会得到意外的输入?@zondo当有人更改它或做任何我不期望的事情时,所以一直以来:)我想说的一点是,最好是防御性的,而不是以你必须修复的坏数据结束,因为计算机花费很少的额外时间来剥离任何东西的成本要低得多。