Modified PyQspice *.cir file

Hi Guys,

I am new using the PyQspice tool. I am wondering if in example 10_DC (see link below) would it be possible to modified some parameters on the netlist (*.cir file) such as the input voltage Vds before running the simulation?

#run the schematic
run = pqs(“UJ3N065080”)

#Generate the Netlist
run.qsch2cir()

Best,
Daniel

Hi @daval ,

Please take a look on my PyQSPICE usage example here, I’m using the software for my article writing at Microwave Journal.


In this example of “Sim1 @ Article10”, I’m running below 2 lines to comment “out and in” simulation text blocks (section #1 of ReadMe.md).

run.qsch2cir()
run.cir4label('ac')
run.cir4label('tran')

For this example, I prepared my QSPICE schematic (on the GUI schematic capture window), in this purpose in advance, as shown in the schematic capture here.


For a generic / dynamic modification of the netlist, you can just use the regular expression routines of Python.

import re

After calling the “qsch2cir” function, you have your “.cir” file and it’s all yours to modify.

In the PyQSPICE library, above “cir4label” function is just substituting its .cir contents by the RE routines shown as below.

Here, please note one important thing:
Opening your “.cir” file as SJIS in your Python code.
It is not intended to be SJIS but, by default, Python makes a majority-vote / higher-probability decision that a QSPICE “.cir” is SJIS and using SJIS reduces number of warnings you may receive during your coding.

def cir4label(self, label = 'simulation_label'):
                if label == 'simulation_label': return

                in_label = ""
                lines = ""

                with open(self.path['cir'], encoding='SJIS') as f:
                        for line in f:
                                line = line.rstrip('\r\n')
                                if ret := re.match(r'^\*\s*PyQSPICE\s+(\S+)\s+end\s*', line):
                                        in_label = ""
                                if ret := re.match(r'^\*\s*PyQSPICE\s+(\S+)\s+begin\s*', line):
                                        in_label = ret.group(1)
                                        if re.match(label, in_label, flags=re.IGNORECASE):
                                                line = r'*' + line
                                                in_label = label
                                        else:
                                                in_label = "other"
                                if in_label != "":
                                        if in_label == label:
                                                line = re.sub(r"^\*(.*)$", r"\1", line)
                                        else:
                                                line = re.sub(r"^\.(.*)$", r"*.\1", line)
                                lines = lines + line + '\n'

                ofile = self.path['base'] + "." + label + ".cir"
                with open(ofile, 'w') as f:
                        f.write(lines)
                        clsQSPICE.tstime(self, [label + '.cir'])

                self.sim['label=' + label] = {'label': label, 'Nline': self.sim['Nline'], 'Nbit': self.sim['Nbit']}
                self.sim['labels'] += [label]

Hope this helps, please feel free to continue this thread!

Hi Masasshi,

Thanks for your help. I tried to run our example, but I am getting an error related to the encoding of the schematic file


UnicodeEncodeError Traceback (most recent call last)
Cell In[6], line 21
19 run = pqs(fname)
20 run.qsch2cir()
—> 21 run.cir4label(‘ac’)
22 run.cir4label(‘tran’)

File ~\AppData\Roaming\Python\Python311\site-packages\PyQSPICE\qspice.py:146, in clsQSPICE.cir4label(self, label)
144 ofile = self.path[‘base’] + “.” + label + “.cir”
145 with open(ofile, ‘w’) as f:
→ 146 f.write(lines)
147 clsQSPICE.tstime(self, [label + ‘.cir’])
149 self.sim[‘label=’ + label] = {‘label’: label, ‘Nline’: self.sim[‘Nline’], ‘Nbit’: self.sim[‘Nbit’]}

File C:\ProgramData\Anaconda3\Lib\encodings\cp1252.py:19, in IncrementalEncoder.encode(self, input, final)
18 def encode(self, input, final=False):
—> 19 return codecs.charmap_encode(input,self.errors,encoding_table)[0]

UnicodeEncodeError: ‘charmap’ codec can’t encode character ‘\uff75’ in position 22: character maps to

I modified qspice at line 146 to open the file using encoding=“utf-8”, but I am still getting the same error above. Any thoughts?

Best,
Daniel

@daval

From your post, I assume you can modify the qspice.py temporarily on your environment.
Please try the line #145 this way.

with open(cir, mode="w", encoding='SJIS') as f:

Then, please kindly let me know if this works…so to update the PyQSPICE.


The QSPICE netlist is not utf-8 (it’s Latin-1), but Python judges it’s SJIS.

please check this post.