Python Challenge-Level 2

http://www.pythonchallenge.com/pc/def/ocr.html


http://wiki.pythonchallenge.com/index.php?title=Level2:Main_Page php

#!/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()
相關文章
相關標籤/搜索