I hope you can help me with this tough task, where I need to convert the lines inside a txt file into a csv format.
Input:
Network object group 1
Description ADDED
host 10.1.6.174
Network object group 2
Description 2
Network object group A
Description 3
host 10.1.4.4
host 10.1.4.5
host 10.1.4.6
host 10.1.4.8
Network object group D
Description 4
host 10.203.1.16
host 10.203.1.15
host 10.203.1.119
host 10.203.1.80
host 10.203.1.215
to a csv like:
Network object group 1,Network object group 2,Network object group A,Network object group D
Description ADDED, Description 2 , Description 3, Description 4
host 10.1.6.174,, host 10.1.4.4, host 10.203.1.16
,, host 10.1.4.5, host 10.203.1.15
,, host 10.1.4.6, host 10.203.1.119
,, host 10.1.4.8, host 10.203.1.80
,,, host 10.203.1.215
Note that there's always a Network group and a Description, but the host part may or not exist , so it can be of any size. ps: these are "standard Cisco's network object group configurations"
Thanks a lot in advance.
CodePudding user response:
I'd parse the network groups as a list of data structures (for simplicity, I'll use a dict
with entries "name"
for the name, "desc"
for the description, and "hosts"
for a list of hosts).
To output those network groups in a transposed way, i.e. each group in a column, you can iterate the list for each line of output, printing only the corresponding data in each line:
import sys
def parse_network_groups(file) -> list[dict]:
groups = []
for line in file:
line = line.strip()
if line.startswith("Network object group "):
current_group = {"name": line, "hosts": []}
groups.append(current_group)
elif line.startswith("Description "):
current_group["desc"] = line
elif line.startswith("host "):
current_group["hosts"].append(line)
else:
raise NotImplementedError("Cannot parse " line)
return groups
def print_network_groups(groups: list[dict]) -> None:
print(",".join(group["name"] for group in groups))
print(",".join(group["desc"] for group in groups))
for i in range(max(len(group["hosts"]) for group in groups)):
print(
",".join(get_element_or_default(group["hosts"], i, "") for group in groups)
)
def get_element_or_default(list: list[str], index: int, default: str) -> str:
try:
return list[index]
except IndexError:
return default
if __name__ == "__main__":
network_groups = parse_network_groups(sys.stdin)
print_network_groups(network_groups)
Note that I didn't focus on the CSV generation, but just use ",".join(values)
– which may be good enough if the values
don't contain ,
characters – as the question was more about the logic part.
CodePudding user response:
You can use pandas to save your data as a csv. first you need to convert the data into a python Dict. Then you can use save it as a pandas DataFrame and then you could use to_csv
to convert it into a csv file.
import pandas as pd
pd.DataFrame(data).to_csv("file_name.csv")
Above the variable data should be a python dictionary.
example
data = {'column1':[1,2,3,4,5,6,7,8,9,10],'column2':[2,3,41,56,7,4,5]}