Python - Remove the last character in a netlist file

93 views Asked by At

I m generating a netlist file from a csv file as you can see below and i m trying to suppress the last character "," of this file text after generating it but i cant

module Filtre(E[0],E[1],E[2],E[3],S[0],S[1],S[2],S[3],);

Expected result:

module Filtre(E[0],E[1],E[2],E[3],S[0],S[1],S[2],S[3]);

My fonction to generate the netlist file :

fichNet = open('Netlist.v', 'r+')
def readColCSV(fichier):
    with open(fichier, 'r') as read_obj:
        csv_dict_reader = DictReader(read_obj)
        entete = csv_dict_reader.fieldnames[0]
        fichNet.writelines('\nmodule {}('.format(entete))
        for row in csv_dict_reader:
            if 'E' in row['pin name'] or 'S' in row['pin name']:
                fichNet.writelines('{},'.format(row['pin name']))
        fichNet.writelines(');\n\n')
2

There are 2 answers

0
sai Pavan Kumar On BEST ANSWER

It will work, create a empty list, add your strings to the list and join with “,” so that it won’t write “,” at the end of your string.

fichNet = open('Netlist.v', 'r+')
def readColCSV(fichier):
    with open(fichier, 'r') as read_obj:
        csv_dict_reader = DictReader(read_obj)
        entete = csv_dict_reader.fieldnames[0]
        fichNet.writelines('\nmodule {}('.format(entete))
        #Add here
        l=[]
        for row in csv_dict_reader:
            if 'E' in row['pin name'] or 'S' in row['pin name']:
                #Change here
                l+=[row['pin name']]
        #Change here
        fichNet.writelines(','.join(l)+');\n\n')
0
Red On

Change your function to this:

fichNet = open('Netlist.v', 'r+')
def readColCSV(fichier):
    with open(fichier, 'r') as read_obj:
        csv_dict_reader = DictReader(read_obj)
        entete = csv_dict_reader.fieldnames[0]
        fichNet.writelines('\nmodule {}('.format(entete))
        for row in csv_dict_reader:
            if 'E' in row['pin name'] or 'S' in row['pin name']:
                if csv_dict_reader.index(row) != csv_dict_reader[-1]:
                    fichNet.writelines('{},'.format(row['pin name']))
                else:
                    fichNet.writelines('{}'.format(row['pin name']))
        fichNet.writelines(');\n\n')