I am trying to convert PYTHON code to TCL


This Content is from Stack Overflow. Question asked by Santhosh Nayak D.

I have two files and I am comparing specific lines between two files using the def function.

def readPinFile(filename): 

   result = None
   with open(filename, "r") as file:    

    result = {}
    lastPin = None        
    for line in file:
        lines = line.strip()
        if lines[:3] == "PIN": 
            lastPin = lines.split(" ")[1]               
            result[lastPin] = {"LAYER": None, "RECT": None}
        if lines[:5] == "LAYER":
            result[lastPin]["LAYER"] = lines.split(" ")[1]
        if lines[:4] == "RECT":
            result[lastPin]["RECT"] = lines.split(" ")

return result

pin_of_file1 = readPinFile("osi_hbmp_top_briscm_1.lef") #lef file1
pin_of_file2 = readPinFile("osi_hbmp_top_briscm_2.lef")#lef file2

The TCL code I tried to get the same output

proc fileinput {filename} {
set filedata [open filename r]
set file1 [ read $filedata ]
foreach line [split $file1 n] {
      set pindata { PIN { LAYER {} RECT {} }}
      if {[string match *PIN* $line]} {
          dict lappend pindata PIN $line         
      if {[string match *LAYER* $line]} {
           dict lappend pindata PIN {LAYER{$line}} 
      if {[string match *RECT* $line]} {
           dict lappend pindata PIN {RECT{$line}}

When I am running my code I am getting errors like this
invalid command name “fileinput{osi_hbmp_top_briscm_1.txt}}”
while executing
(file “aa.tcl” line 18) line: fileinput{osi_hbmp_top_briscm_1.txt}}

can anyone tell me what wrong I did and how to clear that error?


This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?