Python readline将我的打印定位在下面,而不是旁边



所以我想把Yuppie或Noup的文本放在readline打印的右侧,但我还不太清楚切片和间距。这项任务是关于从预先存在的.txt文件中删除所有特殊字符,无论如何,代码如下:

import string
bringthe = open("ape.txt","r")
readthe = bringthe.readline()
invalid_char = set(string.punctuation)
while readthe:
readthe = readthe[:-1]

readthe = bringthe.readline()
if any(poop in invalid_char for poop in readthe):
print(readthe,'yuppie')
else:
print(readthe,'nouppie')

这就是结果,所有的答案都在错误的位置:

no2no123non4 
nouppie
noq234n5ioqw#%
yuppie
%#""SGMSGSER
yuppie
doghdp5234
nouppie
sg,dermoepm
yuppie
43453-frgsd
yuppie
hsth()))
yuppie
bmepm35wae
nouppie
vmopaem2234+0+
yuppie
gsdm12313
nouppie
bbrbwb55be3"?"#?
yuppie
"?"#%#"!%#"&"?%%"?#?#"?"
yuppie
retrte#%#?%
yuppie
abcdefghijklmnopqrstuvxy  nouppie
nouppie

readline()函数将换行符保留在末尾,您可以使用rstrip():将其删除

readthe = bringthe.readline().rstrip()

相关内容

最新更新