Python readline 将我的印刷品放在下面而不是旁边
Python readline positioning my print underneath instead of next to
所以我想在 readline 打印的右侧显示 Yuppie 或 Noup 的文字,但我还不太清楚切片和间距。此任务是关于从预先存在的 .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()
所以我想在 readline 打印的右侧显示 Yuppie 或 Noup 的文字,但我还不太清楚切片和间距。此任务是关于从预先存在的 .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()