1. 程式人生 > >從規則文本文件中提取列字段

從規則文本文件中提取列字段

inf write pen style tel for str split phone

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)

從規則文本文件中提取列字段