zoukankan      html  css  js  c++  java
  • 从规则文本文件中提取列字段

        test.txt内容如下:
    1|name1|agg1|add1|phone1|card1|
    2|name2|agg2|add2|phone2|card2|

        如何提取第4列的内容,即add1、add2

     1 def iterdatainfile(filename, spliter='	'):
     2     with open(filename, 'rt') as handle:
     3         for ln in handle:
     4             yield ln.split(spliter)
     5  
     6 focue, LF = 3, '
    '  #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)
  • 相关阅读:
    6-1
    4-9
    4-5
    4-4
    4-3
    3-10
    作业三2
    作业三1
    课堂练习二
    实验三
  • 原文地址:https://www.cnblogs.com/sxinfo/p/10392736.html
Copyright © 2011-2022 走看看