1

I have a list containing string from lines in txt file.

import csv
import re
from collections import defaultdict

parameters = ["name", "associated-interface", "type", "subnet", "fqdn", "wildcard-fqdn", "start-ip", "end-ip", "comment"]
address_dict = defaultdict(dict)
address_statements = []

with open("***somepaths**\\file.txt",
          "r") as address:
    in_address = False
    for line in address:
        line = line.strip()
        #print (line)

        if in_address and line != "next":
            if line == "end":
                break
            address_statements.append(line)
        else:
            if line == "config firewall address":
                in_address = True
    #print(address_statements)

    if address_statements:
        for statement in address_statements:

            op, param, *val = statement.split()

            if op == "edit":

                address_id = param
            elif op == "set" and param in parameters:
                address_dict[address_id][param] = ' '.join(val)

# output to the CSV
with open("***somepaths**\\file.csv", "w",
          newline='') as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=parameters)
    writer.writeheader()
    for key in address_dict:
        address_dict[key]['name'] = key
        writer.writerow(address_dict[key])

output should be like this: edit "name test" but it turn out to emit the space after the name and be like this: edit "name

How can I include everything in the double quotes?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
obie
  • 13
  • 3
  • 2
    `address_statements` is always Falsy so your code does nothing. [edit] your post and add a working [mre] that shows your problem. Your code has more problems - f.e. `parameters` is bound to produce a `NameError` – Patrick Artner Dec 11 '19 at 15:46
  • edited and put full code. The code is working despite the issue im facing. My objective is to convert txt file to csv. As an example I have file.txt, containing: -edit "CTS SVR" -set associated-interface "DR_RAS" -set subnet 172.16.17.10 255.255.255.255 -next -edit "Es_DC" -set subnet 172.16.17.20 255.255.255.255 -next The output : edit "CTS SVR". But it turn out to be edit "CTS. The code seems emit the space after the "CTS. How can I include everything in the double quotes. – obie Dec 12 '19 at 05:13
  • Probably a duplicate of [Split a string by spaces preserving quoted substrings](https://stackoverflow.com/questions/79968/split-a-string-by-spaces-preserving-quoted-substrings-in-python) - I closevoted earlier and cannot vote for duplicate anymore - hence the answer. – Patrick Artner Dec 12 '19 at 07:11

1 Answers1

0

You are using

op, param, *val = statement.split()

which splits at spaces - a line of

edit "CTS SVR"'

will put '-edit' into op, '"CTS' into param and the remainder of the line (split at spaces as list) into val: ['SVR"'].

You need a way to Split a string by spaces preserving quoted substrings - if you have params that are internally seperated by spaces and delimited by quote.

Inspired by this answer the csv module gives you what you need:

t1 = 'edit1 "some text" bla bla'
t2 = 'edit2 "some text"'
t3 = 'edit3 some thing'
import csv

reader = csv.reader([t1,t2,t3], delimiter = " ", skipinitialspace = True)
for row in reader:
    op, param, *remainder = row
    print(op,param,remainder, sep = " --> ")

Output:

edit1 --> some text --> ['bla', 'bla']
edit2 --> some text --> []
edit3 --> some --> ['thing']

You can apply the reader to one line only ( reader = csv.reader([line], delimiter = " ") ).


Probably a duplicate of Split a string by spaces preserving quoted substrings - I closevoted earlier on the question and cannot vote for duplicate anymore - hence the detailed answer.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69