python字母数字,Python字母数字

Problem:

I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m345ö34l is alphanumeric"

Example of the text file:

5345m345ö34l

no2no123non4

%#""SGMSGSER

My code is as follows:

file = open('file.txt','r')

data = file.readlines()

for i in data:

i.strip()

if (i.isalnum()):

print (i, 'is alphanumeric')

else:

print (i, 'not alphanumeric')

file.close()

We can see that the first and second line is alphanumeric but the program doesn't work?

解决方案

try this and see if this working -

file = open('file.txt','r')

data = file.readlines()

for i in data:

stripped_line = i.strip()

if (stripped_line.isalnum()):

print (stripped_line, 'is alphanumeric')

else:

print (stripped_line, 'not alphanumeric')

file.close()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值