#!/usr/bin/env python3
import sys
with open(sys.argv[1]) as fd:
for line in fd:
# we're going to store everything into list of words
# and record individual characters into 'item' string
# and rebuild everything as we go.
words = []
item_found = False
item = ""
counter = 0
for char in line:
# if we see ( or [ we start recording chars
# difference is that [ means item already been edited
# so no need to do anything - just put it into words list
# as is
if char == "(" or char == "[":
item_found = True
counter = counter + 1
continue
if char == ")":
item_found = False
if item.replace(",","").isdigit():
words.append("[" + item + "]")
else:
words.append("("+item+")")
item = ""
if char == "]":
item_found = False
item = item + char
words.append("[" + item)
item = ""
if item_found:
item = item + char
# if we didn't see any open brackets or no closing brackets
# just print the line as is - otherwise give us the altered one
if counter == 0 or item_found:
print(line.strip())
else:
print(", ".join(words))
$ # original input file
$ cat input.txt
(1), (3), (1,2,3), (1,2,3,4,5,6,7,8,9), (Fig1) (Fig1,Fig2), (Table-1, Table-2)
(table-25),[1,2,3],(figure-35)
(figure-1),(figure-2)
$ # script output
$ ./change_brackets.py input.txt
[1], [3], [1,2,3], [1,2,3,4,5,6,7,8,9], (Fig1), (Fig1,Fig2), (Table-1, Table-2)
(table-25), [1,2,3], (1,2,3]figurefigure-35)
(figure-1), (figure-2)