在python中将新列添加到文本文件中

时间:2016-04-04 00:46:35

标签: python parsing

我是python的新手。我试图解析一个文件并过滤所需的数据并创建一个文本文件。我想要做的下一步是在文本文件中添加一个指示函数类型或变量的列。我无法获得确切的输出。我确实解析了文件过滤器,最后一步是添加额外的列,其中包含" i"分配给带参数的函数," v"对于无效功能和" a"对于像输入文件这样的变量将包含

0001016a g     F .text  00000016 data_init(unsigned int, unsigned int, unsigned int)
2007c234 g     O .bss   00000004 servo_turner_claw2
2007c140 g     O .data  00000004 _impure_ptr
2007c000 g       .data  00000000 _data
2007c5bc g       .bss   00000000 _pvHeapStart
00010c4c g     F .text  00000018 open_claw2()
2007c1bc g       .data  00000000 _edata
00010278 g     F .text  00000004 operator new(unsigned int)
2007c0dc g     O .data  00000004 __ctype_ptr__
2007c5ac g     O .bss   00000004 __malloc_sbrk_start
2007c5b0 g     O .bss   00000004 __malloc_free_list
00010c9c g     F .text  00000024 rotate_claw2_anticlkwise()

预期的输出是

a  2007c140  .data  _impure_ptr
a  2007c000  .data  _data
a  2007c5bc  .bss  _pvHeapStart
v  00010c4c  .text  open_claw2()
a  2007c1bc  .data  _edata
i  00010278  .text  operator  new(unsigned  i  00010278  .text  operator  new(unsigned  i  00010278  .text  operator  new(unsigned  i  00010278  .text  operator  new(unsigned  a  2007c0dc  .data  __ctype_ptr__
a  2007c5ac  .bss  __malloc_sbrk_start
a  2007c5b0  .bss  __malloc_free_list
v  00010c9c  .text  rotate_claw2_anticlkwise()

我写的代码是

f = open("final_lst.txt", "r+")
g = open("Filtered_lut.txt", "w+")

filter_list = ["int","unsigned"]
for line in f:
    if ".data" in line :
        columns = line.split("  ")
        columns.insert(0, "va")
        g.write("  ".join(columns)+"\t")

    if ".text" in line:
        if "(" in line:
            for word in filter_list:
                columns = line.split("  ")
                columns.insert(0, "vi")
                g.write("  ".join(columns)+"\t")

        if "()" in line:
            columns = line.split("  ")
            columns.insert(0, "vv")
            g.write("\t".join(columns)+"\t")


f.close()
g.close()

0 个答案:

没有答案