【问题标题】:Delete multiple row if match whit string如果与字符串匹配,则删除多行
【发布时间】:2022-10-15 01:02:03
【问题描述】:

我需要帮助,我找不到适合我的案例的特定解决方案,我有以下 txt,我需要用 Pandas 阅读它并删除某些行,然后将其转换为 csv:

Wed Oct 12 12:53:38.816 EDT                                                      #i need delete this row with the date
                                                                                 #i need delete this blank row 
Interface          Status      Protocol    Description
-------------------------------------------------------------------------------- #i need delete this row whith the "-"
Lo0                up          up          Loopback0 interface configured by Netmiko
Lo55               up          up          
Lo100              up          up          ***MERGE LOOPBACK 100****
Lo111              up          up          Configured by NETCONF
Nu0                up          up          
Mg0/RP0/CPU0/0     up          up          DO NOT TOUCH THIS !
Gi0/0/0/0          admin-down  admin-down  
Gi0/0/0/1          admin-down  admin-down  test
Gi0/0/0/1.100      admin-down  admin-down  
Gi0/0/0/2          admin-down  admin-down  Link to P2 configured by Netmiko
Gi0/0/0/3          up          up          Configured by Ansible !!!!!!!!
Gi0/0/0/4          up          up          Updated by Ansible using Jinja Template
Gi0/0/0/5          up          up          Configured by Ansible !!!!!!
Gi0/0/0/6          admin-down  admin-down  Updated by Ansible using Jinja Template
Gi0/0/0/6.11       admin-down  admin-down  

让它保持这样:

Interface          Status      Protocol    Description
Lo0                up          up          Loopback0 interface configured by Netmiko
Lo55               up          up          
Lo100              up          up          ***MERGE LOOPBACK 100****
Lo111              up          up          Configured by NETCONF
Nu0                up          up          
Mg0/RP0/CPU0/0     up          up          DO NOT TOUCH THIS !
Gi0/0/0/0          admin-down  admin-down  
Gi0/0/0/1          admin-down  admin-down  test
Gi0/0/0/1.100      admin-down  admin-down  
Gi0/0/0/2          admin-down  admin-down  Link to P2 configured by Netmiko
Gi0/0/0/3          up          up          Configured by Ansible !!!!!!!!
Gi0/0/0/4          up          up          Updated by Ansible using Jinja Template
Gi0/0/0/5          up          up          Configured by Ansible !!!!!!
Gi0/0/0/6          admin-down  admin-down  Updated by Ansible using Jinja Template
Gi0/0/0/6.11       admin-down  admin-down

尝试从这个开始,但它不起作用并且正则表达式不完整

import pandas as pd

df = pd.read_fwf("file.txt")

df = df[~df[0:].str.contains("Wed")]

print(df)

显然我做错了,因为它不起作用,你能帮我吗?

@MosGeo

您的脚本运行良好,但非常严格,我需要执行一个循环,因为我需要使用 if/else 将此脚本与其他匹配项进行匹配,我试图做这样的事情(但不工作):

    with open("output.txt", "r") as f:
        lines = f.readlines()
    
with open("output2.txt", "w") as f:
    for line in lines:
        if re.match(r"([A-Za-z0-9]+( [A-Za-z0-9]+)+)\-{5}\w", line):
            del line[line]
            f.write(line)

【问题讨论】:

  • 刚刚编辑了答案。

标签: python pandas


【解决方案1】:

不要使用熊猫。只需将其作为文本阅读并删除它们。这是创建新文件的示例。不仅仅是用熊猫阅读它。

with open("test.txt", "r") as file:
    lines = file.readlines()

del lines[3]
del lines[1]
del lines[0]

with open("output.txt", "w") as file:
    file.writelines(lines)

如果你愿意,你也可以在上面保存。

编辑:

with open("input.txt", "r") as f:
    lines = f.readlines()

new_lines = []
for line in lines:
    if re.match(r"([A-Za-z0-9]+( [A-Za-z0-9]+)+)-{5}w", line):
        new_lines .append(line)

with open("output.txt", "w") as file:
    file.writelines(new_lines)
  

【讨论】:

  • 谢谢莫斯吉奥!!我编辑了我的问题以供您回答。
最近更新 更多