简体   繁体   English

Python读取文件并解析所需数据

[英]Python reading a file and parsing the required data

Hi I am reading a file data.txt which is in the below format. 嗨,我正在读取以下格式的data.txt文件。

Last table change time   : 69 days, 0:17:19 ago
Number of table inserts  : 93
Number of table deletes  : 87
Number of table drops    : 0
Number of table age-outs : 0

Port       Neighbor Device ID             Neighbor Port ID           TTL
Et2/1      CN-Arista2                       Ethernet5/1                120
Et2/2      CN-Arista2                       Ethernet5/2                120
Et2/3      CN-Arista3                       Ethernet5/3                120
Et2/4      CN-Arista                       Ethernet5/4                120
Et64/1     CN-Spine2                      Ethernet64/1               120
Ma1        Arista01.Dmech.rack01             Ethernet1                  120

I need out put as follows 我需要如下

Et2/1, CN-Arista2, Ethernet5/1
Et2/2, CN-Arista2, Ethernet5/2
Et2/3, CN-Arista3, Ethernet5/3
Et2/4, CN-Arista, Ethernet5/4
Et64/1, CN-Spine2, Ethernet64/1
Ma1, Arista01.Dmech.rack01, Ethernet1 

I'm not sure about what you're trying to do but maybe this gets you in the right direction: 我不确定您要做什么,但这也许可以使您朝正确的方向前进:

filename = 'myfile.txt'

with open(filename, 'r') as file:
    for line in file:
        print(*line.split(), sep=', ')
    file.close()

Thank you TigerhawkT3 :) 谢谢TigerhawkT3 :)

Try this out: 试试看:

fi = open('data.txt')
fo = open('data.csv')
for line in fi:
     L = line.split()
     S = ' '.join(L[5:8]).replace(',','') +', ' + ', '.join(L[14: : 6]) + '\n'
     fo.write(S)
fo.close()
fi.close()
input = """Port       Neighbor Device ID             Neighbor Port ID           TTL
Et2/1      CN-Arista2                       Ethernet5/1                120
Et2/2      CN-Arista2                       Ethernet5/2                120
Et2/3      CN-Arista3                       Ethernet5/3                120
Et2/4      CN-Arista                       Ethernet5/4                120
Et64/1     CN-Spine2                      Ethernet64/1               120
Ma1        Arista01.Dmech.rack01             Ethernet1                  120
"""


#create a test file
fn_in = "./testin.txt"
fn_out = "./testout.txt"

with open(fn_in, "w") as fo:
    fo.write(input)

with open(fn_in, "r") as fi, open(fn_out,"w") as fo:

    #drop first line
    for line in fi.readlines()[1:]:
        #split without argument treats consecutive spaces as 1 separator
        #drop last field
        fields = line.split()[:-1]
        fo.write(", ".join(fields)+"\n")

did it work? 奏效了吗?

$ cat testout.txt


Et2/1, CN-Arista2, Ethernet5/1
Et2/2, CN-Arista2, Ethernet5/2
Et2/3, CN-Arista3, Ethernet5/3
Et2/4, CN-Arista, Ethernet5/4
Et64/1, CN-Spine2, Ethernet64/1
Ma1, Arista01.Dmech.rack01, Ethernet1

Thank you all for your responses. 谢谢大家的答复。 Below is the final code that works well per my requirement. 以下是根据我的要求运行良好的最终代码。

fn_in = "data.txt"
with open(fn_in, "r") as fi:
    #drop first 7 lines
    for line in fi.readlines()[7:]:
        #split without argument treats consecutive spaces as 1 separator
        #drop last field
        fields = line.split()[:-1]
        if fields:
            print(", ".join(fields))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM