python实现每次处理一个字符的三种方法,python三种方法


本文实例讲述了python每次处理一个字符的三种方法。分享给大家供大家参考。

具体方法如下:

a_string = "abccdea" 
 
print 'the first' 
for c in a_string: 
  print ord(c)+1 
 
   
print "the second"   
result = [ord(c)+1 for c in a_string] 
print result 
 
print "the thrid" 
 
def do_something(c): 
  return ord(c)+1 
 
result = map(do_something ,a_string) 
print result 

打印出的结果如下:

the first 
98 
99 
100 
100 
101 
102 
98 
the second 
[98, 99, 100, 100, 101, 102, 98] 
the thrid 
[98, 99, 100, 100, 101, 102, 98] 

希望本文所述对大家的Python程序设计有所帮助。


python处理文件中的字符串处理问题:

楼上的答案,第一行改成
f1 = open('1.txt')
其他没问题

不过有更简单的写法:

f1 = open('1.txt')

linen = 1

for line in reader:
if not line.find('44.2%') == -1:
print linen
linen +=1

f1.close()

如果对您有帮助,请记得采纳为满意答案,谢谢!祝您生活愉快!
 

python怎从文件中取出一个字符串?

如果文件不大的话
import re
f = open('xxxx.xx','r').read()
fList = re.split(r'\s+', f)

OK,所有的都取到fList列表里了
 

评论关闭