I'm trying to convert from a txt file to another csv file but why can't do how can I need.
I got this txt:
Interface Status Protocol Description
Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2 up up Network Interface
Gi2.101 up up mpls-subinterface-CKTx904949
Gi3 admin down down Network Interface
Lo0 up up
Lo56 up up THIS IS A TEST LOOPBACK
Vi0 up up
and this is my script:
output = open("command.txt", "r")
for line in output.readlines():
lista = re.sub(r'\s{2,}', ";", line)
archivo_csv = open("output-1.txt", "a")
archivo_csv.write(lista ";" "\n")
archivo_csv.close()
output.close()
output = open("output.txt", "r ") #This second "loop" is becose between "Protocol"
lineas = output.readlines() #and "Description" be only one space, the regular
linea_1 = lineas[0] #expression that i did don't work for this case
lista = re.sub(r'\s ', ";", linea_1)
output.seek(0)
output.write(lista)
output.close()
vaciar_archivo = open("output.csv", "w")
vaciar_archivo.close()
output = open("output.txt", "r")
for line in output.readlines():
lista = line
archivo_csv = open("output.csv", "a")
archivo_csv.write(lista)
archivo_csv.close()
output.close()
the output is this:
Interface;Status;Protocol;Description;
;
Gi1;up;up;MANAGEMENT INTERFACE - DON'T TOUCH ME
;
Gi2;up;up;Network Interface
;
Gi2.101;up;up;mpls-subinterface-CKTx904949
;
Gi3;admin down;down;Network Interface
;
Lo0;up;up;;
Lo56;up;up;THIS IS A TEST LOOPBACK
;
Vi0;up;up;;
i can't do this well, i think that i have a problem with the form of the regular expression.
I need this so that the CSV file does not generate blank lines:
Interface;Status;Protocol;Description
Gi1;up;up;MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2;up;up;Network Interface
Gi2.101;up;up;mpls-subinterface-CKTx904949
Gi3;admin down;down;Network Interface
Lo0;up;up
Lo56;up;up;THIS IS A TEST LOOPBACK
Vi0;up;up
Could you help me improve my code?
CodePudding user response:
You can try pandas.read_fwf
:
import pandas as pd
df = pd.read_fwf("your_input_file.txt")
df["Description"] = (
df.iloc[:, 3:].fillna("").astype(str).apply(" ".join, axis=1).str.strip()
)
df = df.iloc[:, :4]
print(df)
df.to_csv("data.csv", index=False, sep=";")
Prints:
Interface Status Protocol Description
0 Gi1 up up MANAGEMENT INTERFACE - DON'T TOUCH ME
1 Gi2 up up Network Interface
2 Gi2.101 up up mpls-subinterface-CKTx904949
3 Gi3 admin down down Network Interface
4 Lo0 up up
5 Lo56 up up THIS IS A TEST LOOPBACK
6 Vi0 up up
and saves data.csv
:
Interface;Status;Protocol;Description
Gi1;up;up;MANAGEMENT INTERFACE - DON'T TOUCH ME
Gi2;up;up;Network Interface
Gi2.101;up;up;mpls-subinterface-CKTx904949
Gi3;admin down;down;Network Interface
Lo0;up;up;
Lo56;up;up;THIS IS A TEST LOOPBACK
Vi0;up;up;
CodePudding user response:
I was studying the solution and it is functional to the case, but I see that it does not delete anything but "copy and paste" and then hide the unwanted columns, I have a similar case but in which I need to delete lists, either by coincidence or explicitly the number of lists, this is the case:
Sat Oct 8 17:57:56.688 EDT
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
Lo200 up up ***MERGE LOOPBACK 200****
Nu0 up up
Mg0/RP0/CPU0/0 up up teststpoc
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
in this case i need to delete the date, the blank row and the row with "----". Can you help me with this case?Thank you Andrej!Cheers!