从规则文本文件中提取列字段
test.txt内容如下:
1|name1|agg1|add1|phone1|card1|
2|name2|agg2|add2|phone2|card2|
如何提取第4列的内容,即add1、add2
1 def iterdatainfile(filename, spliter='\t'): 2 with open(filename, 'rt') as handle: 3 for ln in handle: 4 yield ln.split(spliter) 5 6 focue, LF = 3, '\n' #LF = 3:取第4列 7 with open("output.txt", 'wt') as handle: 8 handle.writelines([row[focue] + LF 9 for row in iterdatainfile('test.txt',spliter='|')]) 10 11 # 下面是显示提取到的内容 12 txtstr = open('output.txt') 13 a = txtstr.read() 14 print(a)