Search code examples
pythoncsvtextnetlist

Python - Remove the last character in a netlist file


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')

Solution

  • 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')