You can open present file somescript.sql'
in read mode. Read each line and process it e.g. if there is a column name then capitalized first latter and replace space by _ This can be done using regular expression. Latter you can delete old file and rename temp file as old-filed name.
script.py:
import os, re
with open("somescript.sql") as i: # open sql file for reading
with open("temp", "w") as o: # tem file for writing
for l in i: # read line by line
c = re.match(r".*\[(?P<col_name>.*)\].*", l) # use re to find col_name
if c: # if column name found
c = c.group('col_name') # change col name
o.write(l.replace('['+c+']', '['+c.title().replace(' ', '_'))+']')
else: # ^^ col name titled and replace every space by _
o.write(l)
os.remove("somescript.sql") # delete old file
os.rename("temp", "somescript.sql") # rename file
I did as follows, I have two files:
answer$ ls
script.py somescript.sql
somescript file is:
answer$ cat somescript.sql
Create Table Data(
[SOME ID] int,
[LAST NAME] varchar(30),
[FIRST NAME] varchar(30),
[TLA THING] smallint,
[TLA THING REMARK] varchar(255)
)
$ python script.py # run script
/answer$ cat somescript.sql
Create Table Data(
[Some_Id] int,
[Last_Name] varchar(30),
[First_Name] varchar(30),
[Tla_Thing] smallint,
[Tla_Thing_Remark] varchar(255)
)
To explain: o.write(l.replace(c, c.title().replace(' ', '_')))
o.write(x)
write a string x
in file
l.replace(c, c.title().replace(' ', '_'))
replace first argument c
that is a column name by second argument c.title().replace(' ', '_')
, Second argument is c
titled followed by space is replaced by _
.