以下代码似乎工作正常,但它在页面末尾添加了句点(。)。如何删除点和来自哪里?感谢您的帮助! 的代码
with open("strings.txt") as inf:
for line in inf:
line = line.rstrip()
if line.isalnum():
print("{} was ok".format(line))
else:
print("{} was invalid".format(line))
应该已生成以下输出:
5345m34534l was invalid.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
gswrgsrdgrsgsig45 was ok.
)/(/)(#=%#)%/ was invalid.
++-+-+--+--+-+>-<+-<<_<-+>>++ was invalid.
您的程序生成了以下输出:
5345m34534l was invalid.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
gswrgsrdgrsgsig45 was ok.
)/(/)(#=%#)%/ was invalid.
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid.
答案 0 :(得分:0)
我不明白您在页面末尾与句点(。)相关的确切问题。但我可以告诉你的是,如果你想删除任何你不想要的角色,即修剪。你需要使用rstrip()函数,它将参数作为“你要从右侧修剪的字符”。例如rstrip( '')。如果您想做任何其他事情,请告诉我们。
你的预期和实际输出看起来一样,所以我不明白你想说什么。
[编辑]发表评论后,我明白了你想说的话:
with open("strings.txt") as inf:
for line in inf:
line = line.rstrip(" .") #added space and .(period) to strip
if line.isalnum():
print("{} was ok".format(line))
else:
print("{} was invalid".format(line))