#encoding:utf-8
#在原文档中查找字符,显示所在行,列。并将该行保存在新文档中
rootfile = raw_input('rootfilename: ')
roottxtfile = open(rootfile, 'r')
newfile = raw_input('newfilename: ')
newtxtfile = open(newfile, 'w')
indexcontent = raw_input('index what? ')
linenum = 0 #行号
rownum = 0 #列号
indexrownum = 0 #find函数查找的起始点
for line in roottxtfile:
linenum += 1
if indexcontent in line:
print 'line: %d' % linenum
rownum = 0
while rownum >= 0:
rownum = line.find(indexcontent, indexrownum)
if rownum >= 0:
print 'row: %d' % rownum
indexrownum = rownum + 1
else:
indexrownum = 0
continue
newtxtfile.write(line)
roottxtfile.close()
newtxtfile.close()
今天需要在txt中抓一些数据,用刚学的的python知识写了一个简单的脚本。
可能代码看着有点蠢,但是基本所需的功能还是实现了。
之后会慢慢添加一些功能。