Python字母数字 [英] Python alphanumeric

查看:306
本文介绍了Python字母数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我必须通过具有字符串行的文本文件,并确定每行是否为字母数字。如果该行是字母数字打印,例如5345m345ö34l是字母数字



文本文件的示例:

 5345m345ö34l

no2no123non4
$ b $#SGMSGSER

我的代码如下:

  file = open('file.txt', ')
data = file.readlines()

for i in data:
i.strip()
if(i.isalnum()):
print(i,'是字母数字')
else:
print(i,'not alphanumeric')
file.close()

=h2_lin>解决方案

试试这个,看看这个工作 -

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

在数据中:
stripped_line = i.strip()
if (stripped_line.isalnum()):
print(stripped_line ,'是字母数字')
else:
print(stripped_line,'not alphanumeric')
file.close()


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()

这篇关于Python字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆