[d]代码去除行号的程序 问题
代码去除行号的程序 问题
我是想把code.txt这个文件里的代码前面的行号给去除了,然后再次打开时要求看到的就是没有行号的代码,可是事与愿违呀,大家帮我解释下这是什么原因?
code.txt里的代码:
8. tlds = ('com', 'edu', 'net', 'org', 'gov')
9.
10. for i in xrange(randrange(5, 11)):
11. dtint = randrange(maxint) # pick date
12. dtstr = ctime(dtint) # date string
13. llen = randrange(4, 8) # login is shorter
14. login = ''.join(choice(lc) for j in range(llen))
15 dlen = randrange(llen, 13) # domain is longer
16 dom = ''.join(choice(lc) for j in xrange(dlen))
17 print '%s::%s@%s.%s::%d-%d-%d' % (dtstr, login,
18 dom, choice(tlds), dtint, llen, dlen)8 tlds = ('com', 'edu', 'net', 'org', 'gov')
我去行号用的程序代码:
import redef codeLineNumberKiller(text): pattern = re.compile(r'^(.*?\d+\.?)') return pattern.sub('',text) infile = open('code.txt', 'r+')for line in infile: s = line.replace(line,codeLineNumberKiller(line)) line.replace(line,'') #删除当前行的内容 infile.write(s) infile.close()
import redef codeLineNumberKiller(text): pattern = re.compile(r'^(.*?\d+\.?)') return pattern.sub('',text) infile = open('code.txt', 'r+')outfile = open('code2.txt', 'w')for line in infile: s = line.replace(line,codeLineNumberKiller(line)) line.replace(line,'') outfile.write(s) outfile.close()infile.close()