http://wiki.pythonchallenge.com/index.php?title=Level2:Main_Page
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import string
import re
files = open(r"C:\Python27\test\ocr.txt","r")
#print files
#print len(files.readlines())
def Sol1():
str = ''
#print files.readlines()
for line in files.readlines():
#print "line is ", line
str += ''.join([x for x in line if x.isalnum()])
#print "str is ", str
files.close()
print "final str is", str
def Sol2():
#f.read([size])size未指定则返回整个文件,如果文件大小>2倍内存则有问题。f.read()读到文件尾时返回“”(空字串)。
#f.readline()返回一行
#f.readline([size])返回包含size行的列表,size未指定则返回全部行
#for line in f: print line #通过迭代器访问
#f.write("hello\n") #如果要写入字符串以外的数据,先将他转换为字符串
#f.tell()返回一个整数,表示当前文件指针的位置(就是到文件头的比特数)
#Load the text into a variable named str_original
str_original = files.read()
#string.letters The concatenation of the strings lowercase and uppercase described below. The specific value is locale-dependent,
#and will be updated when locale.setlocale() is called.
str = filter(lambda x: x in string.letters, str_original)
files.close()
print "final str is", str
def Sol3():
str_original = files.read()
d = dict()
for ch in str_original:
#Now d maps each character to a count of the number of times it appears.
d[ch] = d.get(ch, 0) + 1
str = "".join(ch for ch in d if d[ch]==1)
files.close()
print "final str is ", str
def Sol4():
str_original = files.read()
str = str_original.translate(string.maketrans("",""),"[]{}()#$%^@+!*&_").replace("\n","")
files.close()
print "final str is ", str
def Sol5():
str_original = files.read()
str_list = re.findall(r'[a-z]', str_original)
print "final str is ", "".join(str_list)
if __name__ == '__main__':
#Sol1()
#Sol2()
#Sol3()
Sol5()